[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: The meaning of 'sugar' [Forked from Re: Why do we have ipairs?]
- From: Andrew Starks <andrew.starks@...>
- Date: Wed, 11 Jun 2014 08:32:43 -0500
On Wednesday, June 11, 2014, Roberto Ierusalimschy <firstname.lastname@example.org> 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
(I also fail to see how closures could be sugar for objects, but this is
somewhat out of topic here.)
I'm attempting to establish a useful definition for "sugar." I'm not trying to argue for switch or any other changes. Also, I specifically stated that "switch" generally, not always, has some kind of feature (or perhaps internal optimization) to justify its existence. If it doesn't, then it isn't very good sugar, given that it wouldn't even save very much typing.
As for closures as sugar for table based objects with metamethods: I typed it backwards*. I meant table-objects could be categorized as sugar for closures if, as was demonstrated, a while loop could be categorized as sugar for goto.
It may not matter, except as a tool for understanding. When someone says "sugar," do they mean "syntactic sugar," or is that something else?
Do they mean "anything in the language that could be done with something else in the language?"
Or a slightly more strict definition: "If it compiles to the same byte code as something else, it's sugar?"
I have come to understand that term to mean: some feature in the language that exists for the sole purpose of providing shorthand for common programming patterns, usually to reduce typing.
I don't think "for" or "while" loops fall into that category, even with "goto" because they express different concepts and are different tools with different pros and cons.