lua-users home
lua-l archive

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


> 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