branch_case_else_unreachable.orchid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-- An Orchid program demonstrating the use of the `branch!`, `case!`, `else!`, and `unwind!`
-- keywords.
import! orchid.console;
main! {
-- Edit to be 0 or 1 to change the behavior of the program.
bind! number = 1;
bind! parity = number % 2;
-- The `branch! EXPR { PATTERNS... }` statement is used to introduce branching control flow. `EXPR`
-- is evaluated exactly once, and then matched against the `PATTERNS...` inside the braces.
branch! parity {
-- `case! PATTERN = STMT;` is a pseudo-statement that can only appear inside the braces of a
-- `branch!` statement.
-- Exactly one `case!` will be selected to replace the `branch!` statement with its own
-- statement.
-- Together, all of the `case!`s must exhaustively cover the possible values of the `branch!`
-- expression, and no two `case!`s can overlap.
case! 0 = call! console.write_line "Even parity";
case! 1 = call! console.write_line "Odd parity";
-- In the case that many values can be lumped together under a "default" case, the `else!` keyword
-- can be used.
-- Here we invoke the `unwind! [MESSAGE]` statement, which causes an immediate assertion failure and
-- crashes the program with a stack trace.
else! = unwind! "It should not be possible to have a parity other than 0 or 1";
}
}