Writing Modules

Lua 5.2 has removed the module function for creating modules. It is recommended to return a table instead when defining a module.

We can cleanly define modules by using the shorthand hash table key/value syntax:

MoonScript:

MY_CONSTANT = "hello"

my_function = -> print "the function"
my_second_function = -> print "another function"

{ :my_function, :my_second_function, :MY_CONSTANT}

Lua:

local MY_CONSTANT = "hello"
local my_function
my_function = function()
  return print("the function")
end
local my_second_function
my_second_function = function()
  return print("another function")
end
return {
  my_function = my_function,
  my_second_function = my_second_function,
  MY_CONSTANT = MY_CONSTANT
}

If you need to forward declare your values so you can access them regardless of their written order you can add local * to the top of your file.

results matching ""

    No results matching ""