lua-users home
lua-l archive

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


	Hi Chris,

On Sun, 14 Aug 2005, Chris Marrin wrote:

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 was assuming that module "a" inherits from the global env
so you'll have a chain: _M inherits from a which inherits from _G.

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???
	I do think so (if I'm understanding what you mean).
	Another approach will be to add "a" as a local:

local a = a

	Then you'll have access to the table "a" instead of access to
'a table called "a"'.

		Tomas