lua-users home
lua-l archive

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


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:

  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.)

-- Roberto




--

___________________________________________
Best regards,
Sergey Rozhenko                 mailto:sergroj@mail.ru