Results and propagation

Result<T, E> is ok(value) or err(error). The built-in err.Name("message") constructs an Error with stable name and human context.

Postfix ? unwraps success or returns the same error type from the enclosing result function. Match err(error) when you need its fields.

fn divide(a: int, b: int) -> Result<int, Error> {
  if b == 0 { err.DivisionByZero("zero divisor") } else { ok(a / b) }
}

fn main() {
  println(divide(8, 2))
  match divide(8, 0) {
    ok(value) => println(value),
    err(error) => println(error.name),
  }
}
Output & diagnostics
Expected:
ok(4)
DivisionByZero
Ctrl/⌘+Enter runs the current lesson.