lua-users home
lua-l archive

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


Am 07.04.12 11:53, schrieb Marc Balmer:
> Am 07.04.12 11:27, schrieb Miles Bader:
>> 2012年4月7日17:43 Marc Balmer <marc@msys.ch>:
>>>> Why can't you just make the callback a closure, and reference "data"
>>>> directly from it (without making it a parameter)?
>>>
>>> I am using it in a loop, e.g. like this:
>> ...
>>>                        activateCallback = Callback {
>>>                                item[2], -- the function
>>>                                item[3]  -- data to be passed
>>>                        }
>> ...
>>> So I think a closure would not work here?
>>
>> Not sure why not (though maybe I misunderstand your problem).
>>
>> A closure captures bindings, and Lua's for loop creates new bindings
>> for the loop variables on each entry into the loop body.
>>
>> So a closure like:
>>
>>    activeCallback = function () item[2] (item[3]) end
>>
>> should do the right thing.
> 
> I tried it.  It does not work.

Err, I made a mistake.  It does indeed work.  Man, closures are really
powerful, almost scary (and now I can revert hours of work inventing
that Callback pseudo widget hack... ;)

Thanks for pointing me in the right direction!