Page 1 of 1

don't use lua's module function

Posted: Wed Feb 23, 2011 11:10 pm
by EmmanuelOga
Hello, I just want to share a link to this article:

http://lua-users.org/wiki/LuaModuleFunctionCritiqued

Basically using lua's 5.1.4 module function is no longer recommended and will be removed from 5.2

Re: don't use lua's module function

Posted: Thu Feb 24, 2011 8:32 am
by kikito
Yeah. I never liked it in the first place.

Even when I read about it on PIL, it seemed like a half-baked solution. I'm glad it is being removed.

Re: don't use lua's module function

Posted: Thu Mar 03, 2011 1:58 pm
by poorenglish
FYI
Changes since Lua 5.1

Here are the main changes introduced in Lua 5.2. The reference manual lists the incompatibilities that had to be introduced.

Main changes

yieldable pcall and metamethods
new _ENV scheme
ephemeron tables
new library for bitwise operations
light C functions
emergency garbage collector
Here are the other changes introduced in Lua 5.2:
Language

no more fenv for threads or functions
tables honor the __len metamethod
hex and \* escapes in strings
order metamethods work for different types
no more verification of opcode consistency
hook event "tail return" replaced by "tail call"
empty statement
Libraries

new metamethods __pairs and __ipairs
arguments for function called through xpcall
optional 'mode' argument to load (to control binary x text)
new function loadin
loadlib may load libraries with global names (RTLD_GLOBAL)
new function package.searchpath
optional base in math.log
file:write returns file
closing a pipe returns exit status
os.exit may close state
new option 'isrunning' for collectgarbage and lua_gc
frontier patterns
\0 in patterns
new option *L for io.read
options for io.lines
C API

main thread predefined in the registry
new constants LUA_OK and LUA_ERRGCMM
new lua_compare, lua_arith, and lua_len
new lua_version and luaL_version
lua_pushstring and pushlstring return string
new luaL_testudata and luaL_setmetatable
new luaL_tolstring
new lua_copy
new lua_absindex
new lua_upvalueid and lua_upvaluejoin
nparams and isvarag available in debug API
new lua_Unsigned
Implementation

max constants per function raised to 226
internal (immutable) version of ctypes
simpler implementation for string buffers
udata with finalizers are kept in a separated list for the GC
CallInfo stack now is a linked list
parser uses much less C-stack space (no more auto arrays)
new hash for floats
handling of non-string error messages in the standalone interpreter
generational mode for garbage collection (experimental)

Re: don't use lua's module function

Posted: Thu Mar 03, 2011 5:47 pm
by kikito
tables honor the __len metamethod
This is the one I liked the most. Being able to do #vector instead of vector.module() :)

Re: don't use lua's module function

Posted: Sun Apr 17, 2011 7:50 pm
by Lap
I like the bit library and the emergency garbage collector. Is Love getting updated to 5.2?

Re: don't use lua's module function

Posted: Fri Apr 29, 2011 11:22 am
by TsT
I wonder how build a module ?
I'm often using (in file mymodule.lua):

Code: Select all

local M = {}
M.data = something
function M.func1(...) ... end
function M.func2(...) ... end
module("mymodule")
return M
and use with :

Code: Select all

mymodule = require("mymodule")
-- or
local mymodule = require("mymodule")
I'm also see direct global var affectation like

Code: Select all

local M = {}
M.data = something
function M.func1(...) ... end
function M.func2(...) ... end
mymodule = M
module("mymodule")
What is the best ?
Is there a recommended way to define module for löve ?

Regards,

Re: don't use lua's module function

Posted: Fri Apr 29, 2011 12:38 pm
by Robin
TsT wrote:I wonder how build a module ?
This topic is actually about that you shouldn't use the module() function. You can do something like:

Code: Select all

local M = {}
M.data = something
function M.func1(...) ... end
function M.func2(...) ... end
return M
That's how I usually do things. That or:

Code: Select all

mymodule = {}
mymodule.data = something
function mymodule.func1(...) ... end
function mymodule.func2(...) ... end

Re: don't use lua's module function

Posted: Fri Apr 29, 2011 12:47 pm
by TsT
Oh ok, it's just about the module() use.
Thanks !

Re: don't use lua's module function

Posted: Fri Apr 29, 2011 5:21 pm
by vrld
You can also do this:

Code: Select all

package.loaded[...] = true

local module_var = 'bar'

local function foo() print('foo') end
local function bar() print(module_var) end

return {
    foo = foo,
    bar = bar
}
The package.loaded thing makes sure that the require function does not load the file twice.

Re: don't use lua's module function

Posted: Fri Apr 29, 2011 5:25 pm
by bartbes
require() should fill that field, not module().