[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A Question of Style
- From: Jerome Vuarand <jerome.vuarand@...>
- Date: Mon, 28 Nov 2011 11:03:58 +0100
2011/11/26 Rebel Neurofog <rebelneurofog@gmail.com>:
> On Sat, Nov 26, 2011 at 5:03 PM, Marc Balmer <marc@msys.ch> wrote:
>> Hi
>>
>> I have a stylistic question wrt/ calling Lua "callbacks" from a C
>> program. I see two obvious approaches:
>>
>> 1) Lua code registers callbacks explicitely using a RegisterCallback
>> function that is provided by the C program; the C program later calls
>> the callback function when one is registered.
>>
>> 2) Lua code does not register callbacks, but the callbacks must be
>> functions with a certain name, e.g. "MouseMovedCallback"; C code will
>> then see if a function with the correct name is available in the Lua
>> state, and if so, call it.
>>
>> Are there advantages of one approach over the other? Are there other
>> approaches? If you also use callback written in Lua, which you call
>> from C, I'd like you to share your opinion (and/or experience).
>>
>> I experienced with both forms, I am unsure for which form to go...
>>
>
> I prefer the first case.
> A function may contain also upvalues.
> Here's the code:
>
> local desktop = widget_system.create_desktop ()
>
> -- Case 1
> input.set_mouse_handler (desktop.mouse_move)
>
> -- Case 2
> function mouse_move_handler (dx, dy)
> desktop.mouse_move (dx, dy) -- note, there's no ":" here
> end
AFAICT there is no difference between the two methods with regards to
upvalues. Since the function is not a method, the second case can be
better written as:
-- Case 2
mouse_move_handler = desktop.mouse_move