Answer the question
In order to leave comments, you need to log in
How to set nil to a variable in a Lua ternary expression?
In theory, the code should set nil, but in fact there 2
Is there a way to make it a ternary expression?
local i = 1
local k = 1
local res = ((i == k) and nil or 2)
print(tostring(res))
Answer the question
In order to leave comments, you need to log in
This is not exactly a ternary operator, but rather a combination of and and or .
Expression:
Same as ternary operator (which is not in Lua), only if B is true.
A and B or C
local res = ((i ~= k) and 2 or nil)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question