[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: A Question of Style
- From: Roberto Ierusalimschy <roberto@...>
- Date: Sat, 26 Nov 2011 12:00:42 -0200
> 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?
(2) is simpler, but (1) seems more clean, as it avoids global names.
I see some alternative approaches as combinations of (1) and (2).
For instance, Lua code can register one single callback table,
wherein callbacks must be functions with fixed names. It is simple
to set and get callbacks (just table operations), but it avoids
using the global table. Or you can use a table with a fixed name
(so it creates only one global name).