Language
Errors are values
A fallible function returns a result that its caller must handle. The notation T ! E means a successful T or an error E.
Return a result
fn positive(value: num): num ! str {
if value <= 0 { return Err("expected a positive number") }
return Ok(value)
}
let count = positive(3) catch 1
log `{count}`
A result cannot be silently dropped. Use catch when you have a fallback, match when both paths need work, or try when the caller should handle the error.
Propagate an error
fn doubled(value: num): num ! str {
let valid = try positive(value)
return Ok(valid * 2)
}
The propagating bind valid := positive(value) is shorthand for let valid = try positive(value). Both belong in a context that can propagate the error.
Recover or inspect
match positive(-1) {
Ok(value) => { log `{value}` },
Err(problem) => { log `{problem}` },
}
Catch takes a fallback expression. Use match when recovery needs statements. Errors returned as values are different from VM traps: a trap is fatal to the instance unless handled at a supported task boundary.