7 ms·
Never will I understand ternary operators. As soon as you introduce it, some chuckle heads want to use them everywhere. Worse if the syntax allows nested ternar
by 3eb7988a1663 3mo ago
Never will I understand ternary operators. As soon as you introduce it, some chuckle heads want to use them everywhere. Worse if the syntax allows nested ternarys. I guess it keeps the language open for code golfing, but it otherwise seems like redundant syntax that at best saves a few characters.
- 201984 3mo agoLua basically already has ternary operators anyway since "and" and "or" short circuit. I also don't see the need of adding additional syntax for it. local x = condition ? value_a : value b local x = condition and value_a or value_b
- matheusmoreira 3mo ago> The classic Lua idiom a and b or c has a pitfall when b is nil or false: then c is returned, even when a is truthy. > E.g. true and false or 42 returns 42, whereas true ? false : 42 returns the (expected) false.
- deleted 3mo ago[deleted]
- nmz 3mo agoNo, not basically, it simply doesn't have them, Ternary means three as in it operates on 3 operands, and/or operates on 2 operands. They are also not equivalent. x = a ? b : c # x is b, same as you would if a {x=b} else {x=c} lua and/or a,b,c = true, 1, 2 x = a and b or c -- x is b a,b,c = true, false, 2 x = a and b or c -- x is c The or is dependent on its previous operand, so b will return false and skips to c, even if you meant for it to be b. you must use an if then else. However, you can have more than a ternary, if there is no need for short-circuit evaluation, as in, any of the operand is not a function CALL like c(), and you want to remain inside an expression, then you can do this instead select (select is a native C function, this is faster than the table creation below) x = select(a and 1 or 2, b, c) table creation/selection x = ({false,2})[a and 1 or 2] of course, doing something like local x; if a then x=b else x=c end does not look so bad
- Gualdrapo 3mo agoI guess for the JS case it makes sense to be able to shave a few characters for file shrinking purposes, but generally I'm more biased to code clarity and "self-explainability"
- NuclearPM 3mo agoThat’s what compression is for.
- demilicious 3mo agoThat’s why “if” should just be an expression
- matheusmoreira 3mo agoThis is the best answer in my opinion. Ternary is just sugar for an expressive if. LuaJIT seems to be focusing on adding new syntax though, maintainer might not be amenable to updating existing semantics.
- wavemode 3mo agoI don't think if-expressions have to affect existing semantics. Basically, in the parser you would have two different kinds of AST nodes, one for when the `if` keyword is encountered in statement position and another for when it's encountered in expression position. Right now, `if` in expression position is just a syntax error ("unexpected symbol")
- Joker_vD 3mo agoWell, I believe there could be some complications with parsing related to the fact that Lua grammar doesn't really requires semicolons between the statements. But other than that, yeah, detecting "if" in the expression position is pretty unambiguous. No idea why most languages went with "cond-expr ? then-expr : else-expr" bracketed syntax instead.
- _flux 3mo agoSurely the most likely explanation is familiary from C? But e.g. ml-family languages (like OCaml, F#, Haskell) and Rust just have the *if* expression that has a non-void value. If your language accepts expressions as statements (most do?), then I think that should just be compatible out of the box.
- Joker_vD 3mo agoYes, but why C had that syntax? Oh, right, because it didn't use if-then[-else]-end for the conditional statement, and reusing if(cond)[-else] with prohibited braces would be awkward. Oh, and Lua most famously does not accept expressions as statements. Which, now that I think of it, would actually evade most of the parsing complications.
- hiccuphippo 3mo agoI find it most useful in languages that have non-mutable variables and you want to avoid a mutable variable or an extra function when the value comes from a simple condition.
- otikik 3mo agoIn Lua (and LuaJIT) you can already use `and` and `or`: local x = y and y + 1 or 0 The knuckle heads are already using them everywhere.