lua-users home
lua-l archive

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

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:

      init.lua -- this will require(_NAME..".app"), etc.

                 -- that would be require( (...)..".app"), now.


> [...]

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


and everything they might want is there. I don't want to start our
plugins with 18 lines of

   local blah = require"some.blah"


$ cat > allmymodules.lua
return {
  tcp = require( "tcp.plugin" ),
  udp = require( "udp.plugin" ),
  raw = require( "raw.plugin" ),
  test = require( "test" ),
  other = require( "my.other.module" ),
  -- ...

... which reduces your 18 lines to one line as it is now.