Branches a.k.a If Expressions
At first glance if expressions appear similar to if statements in languages like C or Python:
if (cond1) {
doSomething();
} else {
doSomethingElse();
}
The else part is optional.
If expressions are chained using the elif keyword:
if (cond1) {
onCond1();
} elif (cond2) {
onCond2();
} else {
doSomethingElse();
}
There can be as many elifs as necessary.
General form
The conditionals are checked one by one starting with the first one. When a condition that is true is found the corresponding block is executing.
Ifs as expressions
ifs can be used as expressions and return results using the following general syntax
a := if (cond1) -> (res: Type) {
res'loadCond1;
} elif (cond2) {
res'loadCond2;
} else {
res'loadDefaults;
};
In the above example the if returns a result using the variable res. It is creating with the default value for Type. Each branch in the if chain has access to it. The value of the if is the value of res after the matching block or else is executed.
Shortcuts
ifs can also directly return the value of an expressions:
min := if (a < b) a else b;
Or generally:
if (cond1) expr1
elif (cond2) expr2
else exprElse