When Expression

# simple form
when (GuardSeq1) {
    Body1
}
# multiple whens
when (GuardSeq1) {
    Body1
}

else when (GuardSeq2) {
    Body2
}

else when (GuardSeqN) {
    BodyN
}
# when/else
when (GuardSeq1) {
    Body1
}

else {
    ElseBody
}
# when/else when/else
when (GuardSeq1) {
    Body1
}

else when (GuardSeqN) {
    BodyN
}

else {
    ElseBody
}

The branches of an when-expression are scanned sequentially until a guard sequence GuardSeq which evaluates to true is found. Then the corresponding Body (sequence of expressions) is evaluated.

The return value of Body is the return value of the when expression.

If no guard sequence is true, an if_clause run-time error will occur.

If necessary, the else branch can be used in the last branch, as that guard sequence is always true.

Note

Parenthesis around conditions are optional

Example:

is_greater_than = fn (X, Y) {
    when X > Y {
        true
    }

    else {
        false
    }
}

Difference Between if and when

See the if expression reference for details.

Table Of Contents

Previous topic

If Expression

Next topic

Switch Expression

blog comments powered by Disqus