[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Recipe: setfenv with Lua 5.2.0 work3
- From: GrayFace <sergroj@...>
- Date: Tue, 15 Jun 2010 22:34:13 +0700
I thought upvaluejoin can only join upvalues. How can it be used to
break the link?
On 21.05.2010 19:40, Roberto Ierusalimschy wrote:
For instance, consider the following code:
function foo ()
return a + b
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.)
Sergey Rozhenko mailto:firstname.lastname@example.org