lua-users home
lua-l archive

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


Tomas wrote:

...
Also, has anyone made a proposal for how you can promote all the members of "a" into the namespace of the module that requires "a"?

    Why don't you inherit the symbols from "a" with something
like:

require"a"
_M = {}
setmetatable (_M, { __index = a }) -- inherit from "a"
setfenv (1, _M) -- new environment (global table)

    If "a" inherits from the global table you'll get all global
through it!

Ok, I'm liking this. Now I think my only question is whether all functions defined after the setfenv() inherit the same environment. If so, then I think I can make nice isolated objects this way.

Of course, I think I might have to do something a bit more complex with __index, if I want this module to be able to access the real global table as well, right? I would have to make __index a function which first checked in 'a' then in _G. Or maybe I just want to go through and copy each member of 'a' (or some subset) into _M. then I could do:

    setmetatable (_M, { __index = _G })
    ... add some or all of the members of 'a' to _M ...

then I would be able to see everything I need, wouldn't I???

--
chris marrin              ,""$, "As a general rule,don't solve puzzles
chris@marrin.com        b`    $  that open portals to Hell" ,,.
        ,.`           ,b`    ,`                            , 1$'
     ,|`             mP    ,`                              :$$'     ,mm
   ,b"              b"   ,`            ,mm      m$$    ,m         ,`P$$
  m$`             ,b`  .` ,mm        ,'|$P   ,|"1$`  ,b$P       ,`  :$1
 b$`             ,$: :,`` |$$      ,`   $$` ,|` ,$$,,`"$$     .`    :$|
b$|            _m$`,:`    :$1   ,`     ,$Pm|`    `    :$$,..;"'     |$:
P$b,      _;b$$b$1"       |$$ ,`      ,$$"             ``'          $$
 ```"```'"    `"`         `""`        ""`                          ,P`