don't use lua's module function

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
EmmanuelOga
Citizen
Posts: 56
Joined: Thu Apr 22, 2010 9:42 pm
Location: Buenos Aires, Argentina
Contact:

don't use lua's module function

Post by EmmanuelOga » Wed Feb 23, 2011 11:10 pm

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
--------------------------------------------------------------------------------------------------------
http://EmmanuelOga.com

User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: don't use lua's module function

Post by kikito » Thu Feb 24, 2011 8:32 am

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.
When I write def I mean function.

poorenglish
Prole
Posts: 47
Joined: Thu Sep 24, 2009 1:49 pm

Re: don't use lua's module function

Post by poorenglish » Thu Mar 03, 2011 1:58 pm

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)

User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: don't use lua's module function

Post by kikito » Thu Mar 03, 2011 5:47 pm

tables honor the __len metamethod
This is the one I liked the most. Being able to do #vector instead of vector.module() :)
When I write def I mean function.

User avatar
Lap
Party member
Posts: 256
Joined: Fri Apr 30, 2010 3:46 pm

Re: don't use lua's module function

Post by Lap » Sun Apr 17, 2011 7:50 pm

I like the bit library and the emergency garbage collector. Is Love getting updated to 5.2?

User avatar
TsT
Party member
Posts: 161
Joined: Thu Sep 25, 2008 7:04 pm
Location: France
Contact:

Re: don't use lua's module function

Post by TsT » Fri Apr 29, 2011 11:22 am

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,
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)

User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: don't use lua's module function

Post by Robin » Fri Apr 29, 2011 12:38 pm

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
Help us help you: attach a .love.

User avatar
TsT
Party member
Posts: 161
Joined: Thu Sep 25, 2008 7:04 pm
Location: France
Contact:

Re: don't use lua's module function

Post by TsT » Fri Apr 29, 2011 12:47 pm

Oh ok, it's just about the module() use.
Thanks !
My projects current projects : dragoon-framework (includes lua-newmodule, lua-provide, lovemodular, , classcommons2, and more ...)

User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: don't use lua's module function

Post by vrld » Fri Apr 29, 2011 5:21 pm

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.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

User avatar
bartbes
Sex machine
Posts: 4944
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: don't use lua's module function

Post by bartbes » Fri Apr 29, 2011 5:25 pm

require() should fill that field, not module().

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests