Re: The meaning of 'sugar' [Forked from Re: Why do we have ipairs?]
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: The meaning of 'sugar' [Forked from Re: Why do we have ipairs?]
- From: Tomas Guisasola Gorham <tomas@...>
- Date: 2014年6月13日 15:32:28 -0300 (BRT)
On 2014年6月13日, Roberto Ierusalimschy wrote:
I guess I do not understand what you mean by your above comments, sorry. :(
Bad code:
if x == 43 then ...
elseif x == 67 then ...
...
end
Good code:
-- This is somewhere in your code (probably near the top)
-- Code for Events
MOUSECLICK = 43
KEYPRESS = 67
...
-- this is somewhere else
if x == MOUSECLICK then ...
elseif x == KEYPRESS then ...
...
end
(If everything is in the same module, you can declare those names as
locals.)
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