lua-users home
lua-l archive

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


On Aug 3, 2010, at 4:01 AM, Quae Quack wrote:

> Can this not be done like so?:
> local MOD = {}
> local _ENV = setmetatable({},{__index=function(t,k) return MOD[k] or
> _G[K] end , __newindex=function(t,k,v) MOD[k] = v end })
> return MOD

Isn't this functionally equivalent (or at least very close) to

local MOD = {}
_ENV = setmetatable(MOD, {__index=_G})
-- stuff
return MOD

?

The other method I've seen is to use a function, but cache anything accessed:

local _G = _ENV
_ENV = setmetatable({}, {__index = function(t, k) t[k] = _G[k]; return t[k] end})
--stuff
return _ENV