lua-users home
lua-l archive

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


On 9/14/2010 9:09 AM, Ryota Hirose wrote:

Is a ternary operator necessary in Lua?  I think NO for almost Lua
programmer.  and/or idiom work fine in almost case.  But, it may cause a
bug which is hard to solve.

Which syntax is good for ternary operator?   I think ? : is best, but :
is an oeprator for method calling, so lexer will face ambiguity.
  if/then/else is better because it has good readability in script,
there is no ambiguity, and the PATCH IS SMALL.


Although it would be mildly helpful for code maintenance (eliminate temporary variables and redundancy), there's no runtime advantage (that I know of) and the "correct" syntax is no clearer now then it's ever been.

I would think that "if-either-or" would be a good choice as its distinct from "if-then-else" and doesn't suggest that there should be an "end" afterwards.

But should this have higher or lower precedence than comma? (As mentioned by David M. a few days ago <http://lua-users.org/lists/lua-l/2010-09/msg00360.html>) What does this statement mean?

A,B = if C either D or E,F

Lower precedence:
if C then A,B = D else A,B = E,F end

Higher precedence:
if C then A,B = D,F else A,B = E,F end

I think Lua syntax should be as obvious as possible to someone reading it for the first time. That's why ?: is a bad idea, it's gibberish to anyone who isn't already familiar with C. The meaning of the above statement is a stumbling block to people learning Lua because you have to check with the reference manual to know what it means. Most statements don't have that problem, the exception perhaps being "for-in" which carries its own controversy.

So I agree that a ternary operator isn't necessary, nor is there a "bug" to worry about. Just the annoyance of having to explicitly type out if-then-else blocks. But the price of maintaining long-winded code seems cheaper than a potentially confusing syntax.

The real question is, if Lua doesn't get a ternary operator, what will be more of a problem? Responding to requests for a ternary operator on the list, or correcting people who type "LUA"?

--
- tom
telliamed@whoopdedo.org