Comment by 201984
20 hours ago
Lua 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
> 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.
No, 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.
lua and/or
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)
table creation/selection
of course, doing something like
does not look so bad