logoalt Hacker News

Heliodextoday at 1:38 AM3 repliesview on HN

A comment <https://github.com/LuaJIT/LuaJIT/issues/1475#issuecomment-47...> has already been made on the issue regarding the ternary operator, recommending `if x then y else z` over `x ? y : z`. This is exactly how it's done with if-then-else expressions in Luau <https://luau.org/syntax/#if-then-else-expressions>, another language compatible with Lua, and makes it a ton easier to nest (especially with elseif) and I believe still easier to read than `y if x else z`.


Replies

noelwelshtoday at 7:38 AM

Exactly. I don't understand why people think the ternary operator is needed when you can just make `if` an expression instead of a statement. Then there is no new syntax to learn and `if` just becomes more useful.

show 1 reply
drunken_thortoday at 3:03 PM

I think that allowing an if statement to return a value to deal with the ternary introduces a now concept to Lua and that is that the value on the final line of a block is a return value much like Ruby. This changes the logic of the entire language more than adding a ternary. I do prefer the if statement as it allows so much more emergent behaviour, but it does have more implications to consider.

show 1 reply
mjcohentoday at 1:58 AM

The ternary operator is easy to nest if you put each clause on a separate line. Then it looks just like nested if-then-else.

show 1 reply