lua-users home
lua-l archive

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


	Hi Roberto

	I thought it was a "regular local variable", since you defined:

Any chunk is compiled as if surronded by the following code:
local _ENV = <some value>; function (...) <chunk> end

	Thus, I thought the above code (correcting `f2' to `function')
could be rewritten as:

local _ENV = _ENV -- In Lua 5.1 it could be written `local _ENV = _G'
function f1()
  local _ENV = _ENV
  return function()
    _ENV = {}
  end
end

	In this case, I think the inner statement would not change f1's _ENV.
What am I missing?

What gets the surrounding _ENV declaration are *chunks*, not functions.
Everything inside the same chunk (a compilation unit) share the same
_ENV, unless there are explicit declarations of other _ENVs.
	Sure.  However, the "body" of a function is a chunk isn't it?
I thought it should have a different (local) _ENV like the above code?

	Regards,
		Tomás

P.S.: Another function defined inside f1 would share the same upvalue
_ENV with the anonymous (f2) returned function and both could change
the local variable and affect the other:

local _ENV = _ENV -- In Lua 5.1 it could be written `local _ENV = _G'
function f1()
  local _ENV = _ENV
  function unused() _ENV = {} end
  return function()
    _ENV = {}
  end
end

	However I thought these inner functions would only have access
to the _value_ of the outer _ENV...