[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: setfenv/getfenv
- From: Roberto Ierusalimschy <roberto@...>
- Date: Tue, 12 Jan 2010 13:25:36 -0200
I will try to answer the sereval requests for our rationale for the
removal of setfenv/getfenv.
The main motivation was that, in our view, setfenv/getfenv was too
dynamic. Basically, if function A can call function B, it can also
change the environment of B. If function A calls B, B can change the
environment of A. In other words, almost anything can change the
environment of anything.
We tried to restrict such uses. Basically, if you create a new function,
you can set its environment. Otherwise, the function itself should be
responsible for its own environment (using lexical environments).
Most uses easily fit this setting. Others may need extra work. For
instance, more than one user talked about loading a piece of code once
and running it whith different environments. I may be wrong, but
I think the following code does it (suggested by lhf):
code = "string with the code to be loaded"
f = loadstring("in ... do " .. code .. " end")
Now, each time you call 'f', the first argument is the environment where
the code will run. (Of course, if the original code uses the vararg, it
should be corrected to slip this extra first argument.)
We are not sure whether everything that was doable is still doable, but
the main point is that we were not sure whether everything that was
doable should be doable.
-- Roberto
- Follow-Ups:
- Re: setfenv/getfenv, Jerome Vuarand
- Re: setfenv/getfenv, Nicolas
- Re: setfenv/getfenv, Brian Maher
- Re: setfenv/getfenv, Daniel Silverstone
- Re: setfenv/getfenv, Alexander Gladysh
- Re: setfenv/getfenv, steve donovan
- Re: setfenv/getfenv, Richard Hundt
- Re: setfenv/getfenv, Jerome Vuarand
- Re: setfenv/getfenv, Petite Abeille
- Re: setfenv/getfenv, Petite Abeille
- Re: setfenv/getfenv, Cosmin Apreutesei
- Re: setfenv/getfenv, Juris Kalnins
- Re: setfenv/getfenv, Ben Kelly
- Re: setfenv/getfenv, Tobias Kieslich