Errors
A failure is a typed enum to match on, not a string.
Option and Result
The prelude defines Option(a) with Some and None,
and Result(a, e) with Ok and Err.
Option states a value may be absent. Result states an operation
failed, and the failure is a typed enum to match on, not a string. A fallible operation
with nothing to say returns Result(_, Nil).
Fallbacks
or supplies the fallback. Array indexing returns Option, never a crash.
println(users[9] or 'out of range')
The receiver form of or binds the error payload, so recovery reads why it failed.
score = parse_score('oops') or e -> {
match e {
Empty -> println('nothing to parse')
NotANumber(t) -> println('${t} is not a number, using 0')
}
0
}
Chaining
<- chains fallible calls. a <- call(args) followed by the
rest of its block becomes call(args, fn(a) { ... }), so with
result.then each line binds the success value and the first error returns.
The standard library is written this way.
pub fn read_text(path String) Result(String, IoError) {
b <- result.then(read_file(path))
result.replace_err(binary.to_string(b), InvalidData(path))
}