Variables

Hurdy variables work just as in Lua (can be global or local) and are scoped in the same way. However, unlike Lua, Hurdy does not default to global variables, bur rather requires variables to be explicitly declared as either local or global before they can be assigned to. The compiler will give an error if a variable is assigned to without declaring it first.

Note

This restriction only applies when assigning to variables. For all other cases they don’t need to be declared and if they have not been declared as local in the current scope they will be treated as free names and accessed from the current environment.

Local variables

Local variables are declared with the var keyword, and follow the regular rules of local variables in Lua.

var x -- declare variable, can be assigned to later
x = 2

var y = 5 -- declare and assign at the same time

var z = print -- this works even if print has not been declared

{
  var w = true -- w is only defined in here
}

w = false -- compiler will give an error, whas not been declared in scope

The Lua code corresponding to

var x
x = 2

var y = 5

is

local x
x = 2

local y = 5

Just as in Lua, redaclaring a local variable shadows the previous definition within the current scope.

Multiple local variables can be declared/assigned to simoultaneously like in Lua:

var x, y = 1, 2, 3
var w, z

corresponds to the Lua code

local x, y = 1, 2, 3
local w, z

Global variables

Global variables are declared using the global keyword, and can only be declared if the identifier is not already local in the current scope.

global x -- declare variable, can be assigned to later
x = 2

global y = 5 -- declare and assign at the same time

var z = 3
global z -- compiler will give an error, z is already local in scope

{
  var w = true -- w is only defined in here
}

global w -- this is fine, w is not in scope

Multiple global variables can be declared/assigned to simoultaneously like in Lua:

global x, y = 1, 2, 3
global w, z