lua-users home
lua-l archive

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


On Tue, Aug 17, 2010 at 8:59 AM, David Manura <dm.lua@math2.org> wrote:
> I see the desire for that, though I've come to the opinion that
> environments (_ENV) are not that useful in module definition [1-3].
> That is for me the question is more of whether `local M = {} .....
> return M` should somehow be made more implicit rather than `_ENV =`
> being implicit.

In particular, the 'return M' is fairly easy to leave out by accident.

Here is another take on the no-magic module definition pattern:

local M = {}

function M.export() return 42 end

return publish(...,M)

where publish() would
(a) if the consumer of the module did not want global modifications,
just return the module
(b) otherwise would return the module, but also create the necessary
'package.mod' tables.

This could be controlled by some global variable - or perhaps better
via a variant of require(), since one might want strictness within
one's own library and wish to allow users to choose how strict they
wish their modules to be.

steve d.