lua-users home
lua-l archive

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


On Fri, Jun 13, 2014 at 11:32 AM, Tomas Guisasola Gorham
<tomas@tecgraf.puc-rio.br> wrote:
>         I don't need switch:
>
> local event_handler = {
>         MOUSECLICK = function (...) ... end,
>         KEYPRESS = function (...) ... end,
>         ...
> }
> ...
> local callback = event_handler[x]
> if callback then
>   callback(...)
> end
>
>         Don't you use that kind of construction?
>
>         Regards,
>                 Tomás

Yes, this was brought up earlier in the thread. :P But in this case, x
is a string, not a number. (Which was ALSO brought up in this thread
already.)

Splitting the difference:

MOUSECLICK = 43
KEYPRESS = 67

local event_handler = {
    [MOUSECLICK] = function(...) ... end,
    [KEYPRESS] = function(...) ... end
}

local callback = event_handler[x]
...

So assuming that your backend is returning numeric constants instead
of string labels, you can use something like this. But that's not the
same use case; that's defining independent functions that have a
central dispatch location, rather than defining a set of snippets that
all need to conditionally execute in the same context.

/s/ Adam