std.result
The Result enum and its helpers.
std.result defines Result[T, E] and a handful of helpers. Most operations on a Result use match or the try expression directly; the helpers are for cases where you don’t need to take action on Err.
Definition
Section titled “Definition”export enum Result[T, E]: case Ok(value: T) case Err(error: E)Construction uses named payloads: Ok(value=42), Err(error=some_error).
Helpers
Section titled “Helpers”export def is_ok[T, E](r: Result[T, E]) -> boolexport def is_err[T, E](r: Result[T, E]) -> boolexport def unwrap_or[T, E](r: Result[T, E], fallback: T) -> TAll three are explicitly instantiated at the call site:
let r: Result[i32, bool] = Ok(value=10)let v: i32 = unwrap_or[i32, bool](r, 0)try propagation
Section titled “try propagation”The most common Result idiom is not the helpers: it’s try, which short-circuits the current function on Err:
def compute(ok: bool) -> Result[i32, bool]: try close(true) let value: i32 = try read(ok) return Ok(value=value)try expr evaluates expr; if the result is Ok(v), the expression evaluates to v (or to nothing in statement position when the payload is None). If it’s Err(e), the surrounding function returns Err(error=e) immediately. The two Result types must share the same E.
See examples/result-try for the full file this example comes from.