141

Is there anyway to use inline conditions in Lua?

Such as:

print("blah: " .. (a == true ? "blah" : "nahblah"))
RBerteig
  • 41,948
  • 7
  • 88
  • 128
Softnux
  • 2,440
  • 4
  • 20
  • 21
  • 2
    There is a nice article on [lua-users wiki](http://lua-users.org/wiki/TernaryOperator) about ternary operator, together with problem explanation and several solutions. – Marcin May 03 '13 at 05:42

6 Answers6

200

Sure:

print("blah: " .. (a and "blah" or "nahblah"))
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 48
    +1 for the answer. However I don't think this is strictly true -- I don't use LUA -- but I think it shares a "flaw" with this approach over a ternary operator in other languages. Imagine: `(cond and false-value or x)` That will result in the `x` in all cases. –  Apr 02 '11 at 21:02
  • 1
    wouldn't that also print the value of A as well? – corsiKa Apr 02 '11 at 21:02
  • 12
    @glowcoder No. "The conjunction operator (and) returns its first argument if this value is false or nil; otherwise, and returns its second argument. The disjunction operator (or) returns its first argument if this value is different from nil and false; otherwise, or returns its second argument. Both and and or use short-cut evaluation, that is, the second operand is evaluated only if necessary" -- from http://www.lua.org/manual/5.0/manual.html –  Apr 02 '11 at 21:06
  • @pst very cool. I'll be 100% honest and say the most Lua I've ever done consists of rummaging through some WoW addons. Thanks for the detailed explanation (even if it is just copy/paste) :) – corsiKa Apr 02 '11 at 23:46
  • 4
    @pst is correct that if the intent is that `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`. – RBerteig Apr 03 '11 at 08:28
  • 2
    If you are using this form with variables, you probably assume that the second variable is non-false, which means you should write `a and assert(b) or c`. – HoverHell Aug 08 '18 at 13:24
  • 1
    @user166390 I completely agree with your point, but it's "Lua", not "LUA" - it's not an acronym, it's the Portuguese word for moon. – Pharap Dec 02 '18 at 18:58
48

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))
daurnimator
  • 4,091
  • 18
  • 34
12

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.

uknonot
  • 183
  • 2
  • 8
2

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'
Ruks
  • 3,886
  • 1
  • 10
  • 22
1

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
Scrapper142
  • 566
  • 1
  • 3
  • 12
Konrni
  • 89
  • 10
0

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
kaiuri
  • 11
  • 3