Is there anyway to use inline conditions in Lua?
Such as:
print("blah: " .. (a == true ? "blah" : "nahblah"))
Is there anyway to use inline conditions in Lua?
Such as:
print("blah: " .. (a == true ? "blah" : "nahblah"))
Sure:
print("blah: " .. (a and "blah" or "nahblah"))
(cond and false-value or x)
That will result in the x
in all cases. –
Foyer a and false or true
is not going to give the same answer as not a
. This idiom is usually used for cases where the desired value if a
is true cannot be false
or nil
. –
Omnidirectional a and assert(b) or c
. –
Schaffel If the a and t or f
doesn't work for you, you can always just create a function:
function ternary ( cond , T , F )
if cond then return T else return F end
end
print("blah: " .. ternary(a == true ,"blah" ,"nahblah"))
of course, then you have the draw back that T and F are always evaluated.... to get around that you need to provide functions to your ternary function, and that can get unwieldy:
function ternary ( cond , T , F , ...)
if cond then return T(...) else return F(...) end
end
print("blah: " .. ternary(a == true ,function() return "blah" end ,function() return "nahblah" end))
t
is nil
. –
Toluate You can usually do:
condition and ifTrue or ifFalse
but this isn't necessarily the best way to do it. The major reason why is because if ifTrue
is a falsy value (some of the time), ifFalse
will evaluate even if condition
is a truthy value. One way to do it simply without much extra work is:
(condition and {ifTrue} or {ifFalse})[1]
which has the advantage of not only being an expression and not being subject to the problem of ifTrue
being falsy which means it can handle all cases, but also has the advantage of short-circuiting (not evaluating the other expression). No need for extra functions or messing with complex aspects of Lua.
Although this question is fairly very old, I thought it would be fair to suggest another alternative that syntactically appears very similar to that of the ternary operator.
Add this:
function register(...)
local args = {...}
for i = 1, select('#', ...) do
debug.setmetatable(args[i], {
__call = function(condition, valueOnTrue, valueOnFalse)
if condition then
return valueOnTrue
else
return valueOnFalse
end
end
})
end
end
-- Register the required types (nil, boolean, number, string)
register(nil, true, 0, '')
And then use it like this:
print((true) (false, true)) -- Prints 'false'
print((false) (false, true)) -- Prints 'true'
print((nil) (true, false)) -- Prints 'false'
print((0) (true, false)) -- Prints 'true'
print(('') (true, false)) -- Prints 'true'
Note: For tables, however, you cannot use them directly with the above method. This is because each and every table has it's own independent metatable and Lua does not allow you to modify all tables at once.
In our case, an easy solution would be to convert the table into a boolean using the
not not
trick:print((not not {}) (true, false)) -- Prints 'true'
You could just write the if statement in one line, it is no shorthand, inline or ternary operator stuff tho.
if (dummy) then
print("dummy is true")
end
is equal too
if (dummy) then print("dummy is true") end
Lua is deliberately lightweight so it does not have a ternary operator.
There are a couple of ways to get past this including using the and-or idiom. But I think that is bad for multiple reasons. Mainly because beginners don't understand it though.
I suggest using a function:
local function choice(c, t, f)
return c and t or f
end
local s = choice(2 % 2 == 0, "even", "odd")
t
is falsy it returns f
even when c
is truthy –
Avert Have fun :D
local n = 12
do
local x = (n>15)
and print(">15")
or n>13
and print(">13")
or n>5
and print(">5")
end
© 2022 - 2025 — McMap. All rights reserved.