Conditionals
Edit on GitHubThe if
Expression
Code can be executed conditionally using if
expressions.
Single-sided if
The if
expression can be used with no else
in order to execute side effects:
1 | module Main |
An if
with no else
clause always evaluates to Void
.
Double-sided if
An if
expression with an else
clause executes the code in the if
branch and evaluates to the last expression in the branch if the condition evaluates to true
, and vice versa if the condition evaluates to false
. Both branches must typecheck to the same type.
1 | module Main |
As an expression
Since the if
expression evaluates to a value, it can be assigned to a binding like any other value:
1 | module Main |
It can also be used inline, like a ternary operator: