Either
Example
The following two print
statements have identical results.
local ply = Entity( 1 )
print( "Player " .. Either( ply:IsAdmin(), "is", "is not" ) .. " an admin" )
print( "Player " .. ( ply:IsAdmin() and "is" or "is not" ) .. " an admin" )
Output: If Player 1 is admin, it will print "Player is an admin".
Example
A practical example of the behavior of this function in comparison to Lua's "pseudo" ternary operator, demonstrating short-circuit evaluation, and the lack of it when using Either
.
local function printHello()
print( "Hello, world!" )
return "printHello called"
end
local myCondition = true
print( myCondition and "printHello not called" or printHello() )
print( Either( myCondition, "myCondition is true, but printHello was still called", printHello() ) )
Output:
printHello not called
Hello, world!
myCondition is true, but printHello was still called