lua-users home
lua-l archive

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


IIRC, Luiz posted some examples of how to use an _ENV local variable to
more or less emulate setfenv behaviour. I'm sure I saved it somewhere...

Found. Here it is:

Subject:
Re: A review of changes between 5.1 and 5.2-work3
From:
Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
Date:
Thu, 20 May 2010 13:42:26 -0300
To:
Lua list <lua@bazar2.conectiva.com.br>

> No more "in env do ... end". So what's now the preferred idiom for
> executing a given function in different environments?
>
> (passing the desired environment as an argument and setting_ENV at the
> beginning of the called function?)

You could write your function to be run in different environments:

function f(_ENV,...)
	... -- code that never even mentions _ENV!
end

Or you could provide a setter:

do
	local _ENV=_ENV
	function setenv_for_f(env)
		_ENV=env
	end
	function f(...)
		...
	end
end

To be fair, Roberto too suggested passing _ENV... 46 seconds earlier than Luiz did :-)

--
  Enrico