Module: core.result
Source: ./core/result.reef
Overview
core.result - Result types for error handling
Result types represent operations that can succeed or fail. Use Result instead of returning special error values (like -1 or null).
The canonical type is the generic Result[T, E] (mirrors core.option's Option[T]). Construction requires explicit type args because each variant only carries one of the two type parameters:
let r1: Result[int, string] = @Result[int, string].Ok(42) let r2: Result[int, string] = @Result[int, string].Err("bad input")
Query and unwrap functions infer their type args from the argument:
if is_ok(r1) let v = unwrap_ok(r1) else println(unwrap_err(r1)) end if
unwrap_ok/unwrap_err PANIC when called on the wrong variant — check with is_ok/is_err first, or use unwrap_or for a fallback value. Standard usage pairs E with core.error.Error.
Types
Result (enum)
Variants:
Ok(T)Err(E)
Functions
fn is_ok(r: Result[T, E]): bool
Returns true if the result is Ok (success)
fn is_err(r: Result[T, E]): bool
Returns true if the result is Err (failure)
fn unwrap_ok(r: Result[T, E]): T
Extracts the success value from Ok. PANICS if called on Err — check with is_ok first, or use unwrap_or.
fn unwrap_err(r: Result[T, E]): E
Extracts the error value from Err. PANICS if called on Ok — check with is_err first.
fn unwrap_or(r: Result[T, E], default: T): T
Returns the Ok value or a caller-supplied default on Err.
fn map(r: Result[T, E], f: (T) -> U): Result[U, E]
Applies f to the success value, leaving an Err untouched (and its error type E unchanged). f cannot fail — when the transformation itself can fail, use and_then.
The @Result[U, E].Ok(...) spelling is required, not stylistic: a bare
Result_Ok(v) pins only U from its payload, leaving E undetermined, so the
enum types as Result[U, ?] and unifies with nothing. A constructor may be
written bare only when its payload determines EVERY type parameter.
fn and_then(r: Result[T, E], f: (T) -> Result[U, E]): Result[U, E]
Chains a fallible step: applies f to the success value and returns its Result directly, so a failure inside f becomes the failure of the whole chain. An Err short-circuits and f never runs.
This is map for the case where f itself returns a Result — using map there would give a Result[Result[U, E], E].
Generated by reefc doc