zena:result
import {…} from 'zena:result';
Classes
Outcome
sealed class Outcome<T, E>
Result's storable counterpart: the same two arms as a sealed
reference type, for when a result must outlive return position —
an array of per-item outcomes, a field holding a deferred answer.
Ok carries what the inline form's true arm does, Err the
false arm's payload.
Reach for the inline Result<T, E> first: it allocates nothing.
This type is for the positions inline tuples cannot occupy, and
costs one allocation per outcome.
Ok
Err
final class Err<T, E>(error: E) extends Outcome<T, E>
An error outcome.
error: E
Type aliases
Result
type Result<T, E> = inline (true, T, _) | inline (false, _, E)
Result<T, E> — a value that is either an ok payload or an error payload.
Unlike Option
There are three lanes, not two, so that each lane is type-homogeneous: the
discriminant, the ok payload, and the error payload. The unused payload in
each arm is a hole (_).
import { Result } from 'zena:result';
let checkedSub = (a: i32, b: i32): Result<i32, String> => {
if (b > a) {
return (false, _, "underflow");
}
return (true, a - b, _);
};
if (let (true, v, _) = checkedSub(10, 4)) {
// v is 6
}
Inline tuples are return-position-only, so Result<T, E> may name a
function's return type but cannot be stored in a variable, field, or
collection. To keep a result around, store it as an Outcome below,
or match it and store the payload.