[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: Paige DePol <lual@...>
- Date: Thu, 12 Jun 2014 22:14:07 -0500
On Jun 11, 2014, at 7:47 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
>> However, by the logic you're following, are closures sugar for objects? I'd
>> think you'd say 'no', but then where do you draw the line?
>>
>> When something does exactly the same thing as something else, but in a less
>> verbose (and usually more obfuscated) way, then it is sugar. Sugar is
>> syntactic. That's why it's called "syntactic sugar"
>
> Probably I am missing something, because I fail to see what in a
> conventional switch statement (for a language like Lua) could be
> different than sugar for a chain of if-elseif-else (unless you
> are talking about "fall through", in which case you should use
> gotos).
>
> (I also fail to see how closures could be sugar for objects, but this is
> somewhat out of topic here.)
>
> -- Roberto
Roberto, have you looked at my switch patch[1]? It is not just sugar for if-else and it is measurably more performant than if-else as well.
It also allows fall-through, which I am curious how you would implement using only if-else and goto statements without falling into spaghetti code or other ugliness.
If properly implemented a switch statement offers considerably more utility than simple if-else statements, both in terms of performance and readability.
Granted, your point about not having symbolic constants degrading the quality of code is true... however, this problem already exists for if-else statements as well given that Lua has no facility for symbolic constants.
On Jun 11, 2014, at 9:57 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
>> Perhaps what you are saying is that as there are no symbolic
>> constants in Lua, it is not possible to generate optimized switch
>> statements for all but the simplest scenario where the switch cases
>> are literals?
>
> Exactly. (These cases are not only simplest, but usually considered bad
> code. If we add a switch optimized only for literals, its impact in code
> standards for Lua would be a tragedy...)
>
> -- Roberto
I guess I am not understanding why magic numbers in if-else statements are okay, but in a switch statement it would be a tragedy? How else would you store the case values, other than as the literal value you want to look up? Symbolic constants (ie: MOUSE_UP) are for the programmer, the compiler simply changes them for the literal value (ie: 3) before compiling, so I really do find your statement a bit confusing.
~pmd
[1] https://github.com/FizzyPop/lua-patches/tree/master/joint-patches/jump-table%2Bif-shct%2Bcont-loop