lua-users home
lua-l archive

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


On 6/16/11, Patrick Donnelly <batrick@batbytes.com> wrote:
> On Thu, Jun 16, 2011 at 12:03 AM, Lorenzo Donati
> <lorenzodonatibz@interfree.it> wrote:
>> Since this "setting env for a function" seems not too rare (both for
>> efficiency reasons - as Patrick said - or for coping with DSL parsing, as
>> in
>> my usual case), I wonder whether It could be useful to provide a library
>> function such as runin(env, func) that will run func in the provided
>> environment (and maybe this could also bypass the limitation that debug
>> info
>> should be present, removing a source of indeterminacy when using Sergei's
>> replacements).
>
> It's been said elsewhere by the authors that they don't believe any
> type of environment tampering is okay for functions. That would
> clearly include any sort of "runin" function. I agree with this
> mentality.
>
> My proposal corrects what I believe to be a lacking in both Lua 5.1
> and 5.2 where you cannot create multiple closures of the same
> load-compiled function. Setting _ENV in the generator is just icing on
> the cake.
>
>> Probably this could cope with most of the complaints of a missing
>> setfenv/getfenv in 5.2 (I don't remember any other use case coming up in
>> the
>> list which wasn't covered elegantly by other 5.2. features).
>
> As said above, this isn't just a 5.2 problem.
>
> --
> - Patrick Donnelly
>
>

You can define a function like this:
function f(_ENV, a, b)
  print(a,b)
end

and call it like this:
env = {print = function(...) print('myprint',...) end}
f(env,1,2)

Isn't it enough?