Guide
Option, Result & ?
No null, no exceptions. Absence is Option[T], failure is Result[T, E], and the ? operator propagates both without ceremony.
fn parse_price(raw: Text) -> Result[Int, Text]:
match raw.trim().to_int():
case Some(n):
return Ok(n)
case None:
return Err("'{raw}' is not a number")
fn total(a: Text, b: Text) -> Result[Int, Text]:
let first = parse_price(a)?
let second = parse_price(b)?
return Ok(first + second)
fn main() -> Void:
match total(" 12 ", "30"):
case Ok(sum):
print("total: {sum}")
case Err(e):
print("error: {e}")
match total("12", "abc"):
case Ok(sum):
print("total: {sum}")
case Err(e):
print("error: {e}")$ ptc run results.pt
Recorded output of the real compiler
? unwraps Ok/Some or returns the Err/None from the current function — the error types have to line up, and the compiler explains when they don't. unwrap_or(default) covers the "I just want a value" case.