Conditionals
Edit on GitHubThe if Expression
Code can be executed conditionally using if expressions.
if-else
An if expression with an else clause evaluates the given condition; if the condition is true it executes and evaluates to the expression in the if branch, and if the condition is false it executes and evaluates to the expression in the else branch. Both branches must evaluate to the same type.
if is an expression, and therefore can be used like any other expression; in that way, if can be used like a ternary operator from other languages. For example, we can assign an if expression to a binding:
1 | module Main |
Block expressions are frequently used in combination with if expressions in order to execute several expressions together based on a condition, or to improve readability.
1 | module Main |
if-else expressions can be chained together to create more complex branching behavior:
1 | module Main |
Single-sided if
The if expression can also be used with no else in order to execute side effects:
An if with no else clause evaluates to void.
An aside on Void
void is a special value in Grain, and has the type Void. The Void type is used to represent the absence of a meaningful value; it is generally used in situations where an expression executes side effects and does not evaluate to a meaningful value itself.