Control flow & errors
-- Doc example: control flow (when / match / each) and errors (try / recover / raise).
intent: "doc example: control flow and errors"
task grade(score)
when score >= 90
give "A"
otherwise when score >= 70
give "B"
otherwise
give "C"
task label_of(status)
match status
is "open"
give "active"
is "done"
give "archived"
otherwise
give "unknown"
task sum_list(xs)
let total be 0
each n in xs
set total to total + n
give total
task always_fails()
raise("boom")
print("grade(85) = " + grade(85) + ", sum_list([1,2,3]) = " + text(sum_list([1, 2, 3])))
test "when / otherwise when / otherwise"
assert_eq(grade(95), "A")
assert_eq(grade(75), "B")
assert_eq(grade(50), "C")
test "match dispatches on value"
assert_eq(label_of("open"), "active")
assert_eq(label_of("done"), "archived")
assert_eq(label_of("x"), "unknown")
test "each accumulates"
assert_eq(sum_list([1, 2, 3, 4]), 10)
test "try / recover catches; raise re-raises"
let caught be ""
try
raise("boom")
recover err
set caught to err
assert(contains(caught, "boom"))
assert_error(always_fails)
Conditionals
when score >= 90
give "A"
otherwise when score >= 70
give "B"
otherwise
give "C"
match
match dispatches on a value with rich patterns — literals, enum variants, list/map patterns, guards, and _:
match result
is {status: 200, body} -- map field + binder
use(body)
is [first, ...rest] -- list head + tail
handle(first)
is _ -- wildcard
skip()
otherwise
fallback()
Loops
each item in collection iterates; while condition repeats. Prefer the intentional operations (apply/where/reduce) when you're transforming a list.
Errors — try / recover / raise
try
risky()
recover err
log "failed: " + err -- err is the message (text)
raise(err) -- RE-PROPAGATE; without it, recover swallows the error
raise(message) fails deliberately. give and stop are not errors — they pass through try/recover untouched. (fail(...) is for HTTP responses, not runtime errors.)