Difference between revisions of "Ternary Operator"

From GiderosMobile
(Created page with "__NOTOC__ '''Supported platforms:''' File:Platform android.pngFile:Platform ios.pngFile:Platform mac.pngFile:Platform pc.png<br/> '''Available since:''' Gidero...")
 
m
 
(4 intermediate revisions by 2 users not shown)
Line 4: Line 4:
  
 
=== Description ===
 
=== Description ===
Luau introduces a form of ternary conditional using if cond then value else alternative syntax '''https://github.com/Roblox/luau/blob/master/rfcs/syntax-if-expression.md'''.
+
Luau introduces a form of ternary conditional using if cond then value else alternative syntax.
 +
 
 +
See '''https://luau.org/syntax#if-then-else-expressions'''.
  
 
  The '''''if-then-else''''' expression must match '''if <expr> then <expr> else <expr>'''. The operator can also contain an arbitrary number of elseif clauses, like '''''if <expr> then <expr> elseif <expr> then <expr> else <expr>'''''. Unlike if statements, else is mandatory.
 
  The '''''if-then-else''''' expression must match '''if <expr> then <expr> else <expr>'''. The operator can also contain an arbitrary number of elseif clauses, like '''''if <expr> then <expr> elseif <expr> then <expr> else <expr>'''''. Unlike if statements, else is mandatory.
Line 11: Line 13:
  
 
=== Examples ===
 
=== Examples ===
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
local x = if FFlagFoo then A else B
 
local x = if FFlagFoo then A else B
  
Line 18: Line 20:
 
newThing = if FFlagUseNewThing then t.whatever() else nil,
 
newThing = if FFlagUseNewThing then t.whatever() else nil,
 
})
 
})
</source>
+
</syntaxhighlight>
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
-- more trivial example
 
-- more trivial example
 
local x1 = 10
 
local x1 = 10
Line 29: Line 31:
 
local checkanim
 
local checkanim
 
checkanim = if ent.animation.anims[ent.animation.currentanim] then ent.animation.currentanim else g_ANIM_DEFAULT
 
checkanim = if ent.animation.anims[ent.animation.currentanim] then ent.animation.currentanim else g_ANIM_DEFAULT
</source>
+
</syntaxhighlight>
  
 
=== See also ===
 
=== See also ===
'''https://wiki.gideros.rocks/index.php/Examples#TERNARY_OPERATOR_.40hgy29'''
+
'''https://wiki.gideros.rocks/index.php/Ftf_snippets#TERNARY_OPERATOR_.40hgy29'''
  
 
{{GIDEROS IMPORTANT LINKS}}
 
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 00:11, 17 October 2024

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.png
Available since: Gideros 2022.5

Description

Luau introduces a form of ternary conditional using if cond then value else alternative syntax.

See https://luau.org/syntax#if-then-else-expressions.

The if-then-else expression must match if <expr> then <expr> else <expr>. The operator can also contain an arbitrary number of elseif clauses, like if <expr> then <expr> elseif <expr> then <expr> else <expr>. Unlike if statements, else is mandatory.
The result of the expression is the then-expression when condition is truthy (not nil or false) and else-expression otherwise. Only one of the two possible resulting expressions is evaluated.

Examples

local x = if FFlagFoo then A else B

MyComponent.validateProps = t.strictInterface({
	layoutOrder = t.optional(t.number),
	newThing = if FFlagUseNewThing then t.whatever() else nil,
})
-- more trivial example
local x1 = 10
local x2 = 100
local x = if x1>x2 then x1 else x2 -- result x = 100

-- actual project code
local checkanim
checkanim = if ent.animation.anims[ent.animation.currentanim] then ent.animation.currentanim else g_ANIM_DEFAULT

See also

https://wiki.gideros.rocks/index.php/Ftf_snippets#TERNARY_OPERATOR_.40hgy29