[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Recipe: setfenv with Lua 5.2.0 work3
- From: Roberto Ierusalimschy <roberto@...>
- Date: Fri, 21 May 2010 09:40:59 -0300
> > [...]
> > For instance, consider the following code:
> > local a
> > function foo ()
> > return a + b
> > end
> > In function foo, the first upvalue is 'a' and the second is '_ENV'.
> Why isn't it always the first upvalue? It would make things easier if one
> wants to play with the environment...
For two reasons: First, because _ENV is a regular variable. Second,
because we do not want to make things easier for playing with the
environment. (That was the main reason for the removal of setfenv...)
For those wanting to play with the environment, keep in mind that
_ENV is a regular variable. As such, such upvalue is shared among
all functions created under the same declaration of that variable.
If you change the environment of one of them, you change the
environment of all. (You can use debug.upvaluejoin to break this
sharing, but that only makes sense if you want to simulate the
old setfenv behavior.)