Garry's Mod Wiki

Revision Difference

Global.Either#529306

<function name="Either" parent="Global" type="libraryfunc"> <description>A compact 'if then else'. This is *almost* equivalent to (`condition` and `truevar` or `falsevar`) in Lua. ⤶ The difference is that if `truevar` evaluates to false, the plain Lua method stated would return `falsevar` regardless of `condition` whilst this function would take `condition` into account.</description>⤶ <description>An [eagerly evaluated](https://en.wikipedia.org/wiki/Eager_evaluation) [ternary operator](https://en.wikipedia.org/wiki/%3F:), or, in layman's terms, a compact "if then else" statement. ⤶ In most cases, you should just use Lua's ["pseudo" ternary operator](https://en.wikipedia.org/wiki/%3F:#Lua), like this:⤶ ⤶ ```⤶ local myCondition = true⤶ local consequent = "myCondition is true"⤶ local alternative = "myCondition is false"⤶ ⤶ print(myCondition and consequent or alternative)⤶ ```⤶ ⤶ However, in very rare cases, you may find `Either` useful. In the above example, due to [short-circuit evaluation](https://en.wikipedia.org/wiki/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 below.⤶ </description>⤶ <realm>Shared and Menu</realm> <file line="362-L365">lua/includes/util.lua</file> <args> <arg name="condition" type="any">The condition to check if true or false.</arg> <arg name="truevar" type="any">If the condition isn't nil/false, returns this value.</arg> <arg name="falsevar" type="any">If the condition is nil/false, returns this value.</arg> </args> <rets> <ret name="" type="any">The result.</ret> </rets> </function> <example> <description>The following two `print` statements have identical results.</description> <code> 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" ) </code> <output>If Player 1 is admin, it will print "Player is an admin".</output> </example> <example> <description>An example of the differences between Lua's ternary operator and this function.</description> <description>A practical example of the behavior of this function in comparison to Lua's ["pseudo" ternary operator](https://en.wikipedia.org/wiki/%3F:#Lua), demonstrating [short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation), and the lack of it when using `Either`.</description> <code> local myCondition = true⤶ local consequent = false⤶ local alternative = "Second string"⤶ ⤶ print(myCondition and consequent or alternative)⤶ print(Either(myCondition, consequent, alternative))⤶ 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() ) )⤶ </code> <output> ``` Second string⤶ false⤶ printHello not called⤶ Hello, world!⤶ myCondition is true, but printHello was still called⤶ ``` </output> ⤶ </example>⤶ ⤶ </example>