lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Better to not muck with environments at all. The following works just fine in Lua 5.1 and 5.2:

local M = { }

function M.exportedFunction1()
-- code
end

function M.exportedFunction2()
-- code
end

local exportedFunction2 = M.exportedFunction2 -- fast local access to the function

local function privateFunction()
-- code
end

function M.exportedFunction3()
privateFunction()
exportedFunction2()
end

return M

If you insist on mucking with the global environment, you can always add:

_G[ ... ] = M

People sometimes prefer to write all of the functions as locals and end with:

return {
exportedFunction1 = exportedFunction1,
exportedFunction2 = exportedFunction2,
exportedFunction3 = exportedFunction3
}

This allows you to see all of the exports in one place but it also requires writing the names twice. If you don't trust your spelling, you can help a bit by wrapping the right-hand sides in asserts (or using a lint that hunts for global reads).

Mark