std/option
Value-level combinators over T | null and T | Error.
Vader has no nominal Option / Result types : nullability is the structural
union T | null and fallibility is T | Error (an error is any value whose
type implements the Error trait). These free functions are the conveniences
on top of if x == null / if x is Error narrowing: called via UFCS
(x.or(d) on either, r.map(f)) for default / transform / chain, the
cases where a full narrowing block would be noise. Control flow (early
return, branching, propagation) stays with if / match. They replace the
removed ? operator. See docs/adr/0011-errors-and-null-are-ordinary-union-values.md.
Functions
or :: fn<T>(x: T | null, d: T) -> T The value, or d when it is null.
Parameters
| x | T | null | a nullable value. |
| d | T | the fallback returned when x is null. |
Returns
T x narrowed to T, otherwise d.Example
name :: lookup(id).or("anonymous")or :: fn<T>(x: T | Error, d: T) -> T The value, or d when it is an error: the T | Error overload of or
(dispatched by receiver, so x.or(d) reads the same for nullable and
fallible values).
Parameters
| x | T | Error | a fallible value. |
| d | T | the fallback returned when x is an error. |
Returns
T x narrowed to T, otherwise d.Example
port :: parse_int(s).or(8080)or_else :: fn<T>(x: T | null, f: fn() -> T) -> T The value, or f() when it is null: the lazy form of or, for a fallback
that is expensive or has a side effect (evaluated only on the null path).
Parameters
| x | T | null | a nullable value. |
| f | fn() -> T | computes the fallback ; called only when x is null. |
Returns
T x narrowed to T, otherwise f().or_else :: fn<T>(x: T | Error, f: fn(Error) -> T) -> T The value, or f(err) when it is an error: the lazy T | Error overload of
or_else, with access to the error (to log it, or derive the fallback).
Parameters
| x | T | Error | a fallible value. |
| f | fn(Error) -> T | computes the fallback from the error ; called only on the error path. |
Returns
T x narrowed to T, otherwise f(err).and_then :: fn<T, U>(x: T | Error, f: fn(T) -> U | Error) -> U | Error Chain a fallible step onto the ok value ; an error short-circuits (f is not
called). The monadic bind for T | Error.
Parameters
| x | T | Error | a fallible value. |
| f | fn(T) -> U | Error | the next fallible step, applied to the ok value. |
Returns
U | Error f(x) on success, otherwise the original error.