zena:result

zena
import {…} from 'zena:result';

Classes

Outcome

zena
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.

Variants
zena
case Ok
#
zena
case Err
#

Ok

zena
final class Ok<T, E>(value: T) extends Outcome<T, E>
Extends Outcome<T, E>

A value outcome.

Properties
zena
value: T
#
2 inherited members
From Outcome
zena
case Ok
#
zena
case Err
#

Err

zena
final class Err<T, E>(error: E) extends Outcome<T, E>
Extends Outcome<T, E>

An error outcome.

Properties
zena
error: E
#
2 inherited members
From Outcome
zena
case Ok
#
zena
case Err
#

Type aliases

Result

zena
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, this is an inline multi-value union, not a heap type: it compiles to WebAssembly multi-value returns, so returning an ok or an error costs no allocation. That is why it is a type alias rather than a class hierarchy — see docs/design/result-option.md.

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 (_).

zena
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.