Library

std/option

Value-level combinators over T | null and T | Error.

1 file lib/std/option/option.vader 8 exports

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 2 overloads
or :: fn<T>(x: T | null, d: T) -> T

The value, or d when it is null.

Parameters

xT | null a nullable value.
dT 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

xT | Error a fallible value.
dT 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 2 overloads
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

xT | null a nullable value.
ffn() -> 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

xT | Error a fallible value.
ffn(Error) -> T computes the fallback from the error ; called only on the error path.

Returns

T x narrowed to T, otherwise f(err).
map :: fn<T, U>(x: T | Error, f: fn(T) -> U) -> U | Error

Transform the ok value with f ; an error passes through unchanged.

Parameters

xT | Error a fallible value.
ffn(T) -> U applied to the ok value.

Returns

U | Error f(x) on success, otherwise the original error.
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

xT | Error a fallible value.
ffn(T) -> U | Error the next fallible step, applied to the ok value.

Returns

U | Error f(x) on success, otherwise the original error.
unwrap :: fn<T>(x: T | Error) -> T

The value, or panic with the error's own message. Use only when an error is genuinely unrecoverable: it aborts the process.

Parameters

xT | Error a fallible value.

Returns

T x narrowed to T ; never returns on the error path.
expect :: fn<T>(x: T | Error, msg: string) -> T

The value, or panic(msg): like unwrap, with a caller-supplied message.

Parameters

xT | Error a fallible value.
msgstring the panic message on the error path.

Returns

T x narrowed to T ; never returns on the error path.
Written by vader doc. Icons: Codicons, CC BY 4.0. Fonts: Atkinson Hyperlegible, JetBrains Mono, OFL.