[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [ANN] Lua 5.2.0 (work4) now available
- From: Geoff Leyland <geoff_leyland@...>
- Date: Tue, 3 Aug 2010 17:57:24 +1200
On 3/08/2010, at 5:43 PM, David Manura wrote:
> But here's another, IMO practical, idea along the lines of supporting
> "two simultaneous environment tables":
>
> _ENVW, = {}
> _ENV = _G -- this line actually can be omitted but is included for clarity
> function foo() bar() end
> function bar() print'!' end
> return _ENVW -- Note: rename _ENVW to something friendlier like
> _PUBLIC if you want
>
> What this would mean is that any global variable ever written to
> within the current lexical scope (i.e. foo and bar) would be resolved
> to the _ENVW table for both reads and writes, and all other global
> variables that are only read from would be resolved to _ENV. So, the
> above becomes bytecode-equivalent to
>
> local _ENVW = {}
> local _ENV = _G -- this line actually can be omitted but is
> included for clarity
> function _ENVW.foo() _ENVW.bar() end
> function _ENVW.bar() _ENV.print'!' end
> return _ENVW
>
> and also has basically the same bytecode as my current preferred method:
>
> local M = {}; function M.foo() M.bar() end; function M.bar()
> print'!' end; return M
Bytecode equivalence aside, couldn't you achieve this with:
local _ENV = setmetatable({}, { __index = _G}) -- or is that __index=_ENV now?
function foo() bar() end
function bar() print'!' end
return setmetatable(_ENV, nil)
Cheers,
Geoff
- References:
- Re: [ANN] Lua 5.2.0 (work4) now available, Roberto Ierusalimschy
- Re: [ANN] Lua 5.2.0 (work4) now available, Florian Weimer
- Re: [ANN] Lua 5.2.0 (work4) now available, Luiz Henrique de Figueiredo
- Re: [ANN] Lua 5.2.0 (work4) now available, phlnc8
- Re: [ANN] Lua 5.2.0 (work4) now available, Jim Whitehead II
- Re: [ANN] Lua 5.2.0 (work4) now available, phlnc8
- Re: [ANN] Lua 5.2.0 (work4) now available, Quae Quack
- Re: [ANN] Lua 5.2.0 (work4) now available, GrayFace
- Re: [ANN] Lua 5.2.0 (work4) now available, James Graves
- Re: [ANN] Lua 5.2.0 (work4) now available, Jerome Vuarand
- Re: [ANN] Lua 5.2.0 (work4) now available, David Manura