[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: modules, require, magic
- From: Philipp Janda <siffiejoe@...>
- Date: Sat, 22 Oct 2011 00:59:02 +0200
On 21.10.2011 20:49, Sam Roberts wrote:
> [...]
I'd say the single biggest problem with losing module is that we have
to port everything to 5.2, and do it in a way that also works for 5.1
(which will be here a long time).
Just use the proposed module-less module system -- it will work for Lua
5.1 and 5.2 (no matter what is decided for the future of "module")!
[...]
For the common idiom of constructing
modules in 5.2 to immediately require advanced metatable knowledge,
seems unfortunate to me.
local _ENV = setmetatable({}, {__index=_G})
function foo() end
return _ENV
The proposed common idiom for 5.2 is:
local M = {}
function M.f() end
return M
No metatable or environment knowledge required at all for completely
understanding what is happening!
1. It handles constructing module tables including the fields _NAME, _M, and _PACKAGE. This is useful if one cares about these fields, but I note that they don't appear in the pre-defined modules like table and string.
I use this in constructing modules from multiple files:
zbee/
init.lua -- this will require(_NAME..".app"), etc.
-- that would be require( (...)..".app"), now.
app.lua
link.lua
> [...]
2. It handles constructing submodules. I might question whether it makes sense to have socket.http available when socket is potentially just a stub table, but that's a more complicated matter.
I find hierarchical namespacing useful. I like that we can have
fw/pump/tcp.lua, and that tcp is not the same as
protocol.tcp.lua. And doing
local fwpumptcp = require"fw.pump.tcp"
local protocoltcp = require"protocol.tcp"
strikes me as awkward and silly.
You could also write:
local protocol = { tcp = require"protocol.tcp" }
The point is: The user of the module has the choice!
[...]
Our code doesn't use the global debug, but we do require our own
frameworks, and we want them globally available, so testcase plugins
can
require"tcp.plugin"
and everything they might want is there. I don't want to start our
plugins with 18 lines of
local blah = require"some.blah"
statements.
$ cat > allmymodules.lua
return {
tcp = require( "tcp.plugin" ),
udp = require( "udp.plugin" ),
raw = require( "raw.plugin" ),
test = require( "test" ),
other = require( "my.other.module" ),
-- ...
}
^D
... which reduces your 18 lines to one line as it is now.
[...]
Cheers,
Sam
Philipp
- References:
- modules, require, magic, Eduardo Ochs
- Re: modules, require, magic, Javier Guerra Giraldez
- Re: modules, require, magic, Petite Abeille
- Re: modules, require, magic, Sam Roberts
- Re: modules, require, magic, David Manura
- Re: modules, require, magic, Mark Hamburg
- Re: modules, require, magic, Sam Roberts