Garry's Mod Wiki



any Either( any condition, any truevar, any falsevar )


An eagerly evaluated ternary operator, or, in layman's terms, a compact "if then else" statement.

In most cases, you should just use Lua's "pseudo" ternary operator, like this:

local myCondition = true local consequent = "myCondition is true" local alternative = "myCondition is false" print(myCondition and consequent or alternative)

In the above example, due to short-circuit evaluation, consequent would be "skipped" and ignored (not evaluated) by Lua due to myCondition being true, and only alternative would be evaluated. However, when using Either, both consequent and alternative would be evaluated. A practical example of this can be found at the bottom of the page.

Falsey values

If consequent is "falsey" (Lua considers both false and nil as false), this will not work. For example:

local X = true local Y = false local Z = "myCondition is false" print(X and Y or Z)

This will actually print the value of Z.

In the above case, and other very rare cases, you may find Either useful.


1 any condition
The condition to check if true or false.
2 any truevar
If the condition isn't nil/false, returns this value.
3 any falsevar
If the condition is nil/false, returns this value.


1 any
The result.


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".


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() ) )
printHello not called Hello, world! myCondition is true, but printHello was still called

Special Pages



Render Time: 91ms

DB GetPage 68
Generate Html 8
SaveChanges (1) 5
Render Body 0
Render Sidebar 6