lua-users home
lua-l archive

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


On Sat, Jan 16, 2010 at 7:10 PM, Mark Hamburg <mark@grubmah.com> wrote:
>> setfenv(function()
>>    x = 1
>>    y = 2
>>    g = function() return x+y end
>>    z = 2
>> end,t)()
>>
>> assert(t.g() == 3)
>>
> This, by the way, is a case where dynamic scoping would hurt. t.g() would generally result in an error because definitions for x and y could not be found.

It works precisely because the new function g acquires the environment
t. So as long as that's fine, it works (tested on Lua .5.1.4)