Comment by nmz
3 hours ago
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.
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
No comments yet
Contribute on Hacker News ↗