lua-users home
lua-l archive

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


2010/8/2 James Graves <ansible@xnet.com>:
> On Mon, Aug 02, 2010 at 06:30:39AM +0700, GrayFace wrote:
>> local _G = _ENV
>> local _ENV = {}
>> function foo()
>>    return _G.print("Hello phlnc8!")
>> end
>> return _ENV
>
> In the first case using setmetatable(), would it be advisable to
> delete the metatable before returning _ENV?  So the code would then
> be:
>
> local _ENV = setmetatable({},{__index=_ENV})
> function foo()
> end
> return setmetatable(_ENV, nil)

You can use newindex to perform something similar :

local _M = {}
local _ENV = setmetatable({},{__index=_ENV, __newindex=_M})
function foo()
end
return _M