[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: modules, require, magic
- From: Roberto Ierusalimschy <roberto@...>
- Date: Fri, 21 Oct 2011 16:52:17 -0200
> Many of the complaints against module() are actually against
> package.seeall. The issues of exposing unrelated globals through a
> module and inheriting dependencies are caused by it. The module()
> implementation you suggested leaves this question open, so my
> suggestion in that front would be something along the lines of:
>
> ---------- variation on Roberto's module
> function module (name, ...)
> local env = package.loaded[name]
> if env == nil then
> env = {}
> package.loaded[name] = env
> end
> env._NAME = name
> env._M = env
> for _, f in ipairs{...} do
> env = f(env) -- <=== allow f to redefine env
> end
> return env
> end
With the use being "_ENV = module(...)", we actually do not need the
vararg stuff. We can simply write the modifier explicitly; for instance:
_ENV = seebase(module(...))
BTW, something that seems to be going unnoticed is that, in 5.2 beta,
require calls the loader with two arguments, instead of only one:
http://www.lua.org/work/doc/manual.html#pdf-require
[...]
Once a loader is found, require calls the loader with two arguments:
modname and an extra value dependent on how it got the loader. (If the
loader came from a file, this extra value is the file name.)
Something like "module(..., seeall)" would throw away that second
argument.
-- Roberto
- References:
- Re: modules, require, magic, Petite Abeille
- Re: modules, require, magic, Sam Roberts
- Re: modules, require, magic, David Manura
- Re: modules, require, magic, Hisham
- Re: modules, require, magic, Roberto Ierusalimschy
- Re: modules, require, magic, Fabio Mascarenhas
- Re: modules, require, magic, Roberto Ierusalimschy
- Re: modules, require, magic, Hisham
- Re: modules, require, magic, Roberto Ierusalimschy
- Re: modules, require, magic, Hisham