No matching definitions.

tur/result

stdlib/result.tur
defdata

Result

(defdata Result :copy [A B] (Ok A) (Err B))

typed Result[A B]: ok value A or err value B.

Parameterized result type, as a REAL SUM (SR2b of docs/archive/sum-representation-plan.md): (Ok A) or (Err B). Runtime layout is the tagged monomorph -- { int tag; union { Ok; Err } } -- 16 bytes (down from the record's 24), one payload slot, no dead arm to zero-fill, so err no longer needs a zero value for A nor ok one for B. A concrete monomorph flows BY VALUE with no per-ctor allocation (SR2a, default since 2026-08-27); an erased generic base still rides the int64 carrier, a heap pointer to the same layout. Tag values follow declaration order: Ok = 0, Err = 1. The inline-C helpers in the codegen preamble (tur_box_ok / tur_box_err / tur_is_ok / tur_ok_value / tur_err_value and the _int/_ptr builders) build and read this exact layout; keep them in lockstep with this declaration.

Result -- parameterized result type with ok type A and err type B.

Since: Phase TC1

defn

ok

(ok [A B])

wrap a success value of type A in a Result[A B].

xthe ok value (type A)

A Result[A B] carrying the Ok variant. B is a phantom tyvar inferred from the call site (or pinned with an ascription such as (:: (ok x) (Result A B))).

(ok 42)                        ; => Result int B  (B inferred)
(:: (ok "hi") (Result cstr cstr))
converted to a sum constructor in SR2b -- the dead err arm, and the
`(default-of B)` zero it required, are gone).

Since: Phase TC1 (made polymorphic over A/B as Prereq 3, 2026-06-12;

defn

err

(err [A B])

wrap an error value of type B in a Result[A B].

ethe error value (type B)

A Result[A B] carrying the Err variant. A is a phantom tyvar inferred from the call site (or pinned with an ascription such as (:: (err msg) (Result A B))).

(err 404)                       ; => Result A int   (A inferred)
(:: (err "missing") (Result int cstr))
constructor since SR2b -- no more `(default-of A)` for the dead ok arm).

Since: Phase TC1 (made polymorphic as Prereq 3, 2026-06-12; sum

defn

ok?

(ok? [A B])

check whether a Result[A B] is ok.

rResult[A B]

true if r is ok, false if r is err.

(ok? (ok 1))   ; => true
(ok? (err 0))  ; => false

SR2a graduation: typed (Result A B), not the :int carrier stand-in. ok-val / err-val / some? were already parametric; these two were the last carrier-typed Result accessors, and once a concrete (Result A B) monomorph flows BY VALUE an :int parameter is a straddle the caller has to spill across rather than a harmless erasure. A match body specializes per monomorph and still accepts a carrier value through the generic base.

Since: Phase TC1

defn

err?

(err? [A B])

check whether a Result[A B] is err.

rResult[A B]

true if r is err, false if r is ok.

(err? (err 1))  ; => true

Since: Phase TC1

defn

ok-val

(ok-val [A B])

extract the ok value from a typed Result[A B].

rResult[A B]

The ok value of type A; the zero value of A when r is err (preserving the pre-sum behaviour, where err's dead ok slot held (default-of A)).

(let [r (:: (ok 7) (Result int cstr))]
  (ok-val r))  ; => 7

Since: Phase TC1

defn

err-val

(err-val [A B])

extract the error value from a typed Result[A B].

rResult[A B]

The error value of type B; the zero value of B when r is ok.

(let [r (:: (err "boom") (Result int cstr))]
  (err-val r))  ; => "boom"

Since: Phase TC1

defn

result-free

(result-free [r :int])

free a heap-allocated Result[A B].

rResult[A B] to free
(result-free my-result)

Since: Phase TC1

defn

result-map

(result-map [A B E])

apply a function to the ok value if ok.

rResult[A B]
ffunction A -> C

Result[C B] with f applied to the ok value if ok, or the unchanged err otherwise.

(result-map (ok 3) (fn [x] (* x 2)))  ; => ok(6)
(result-map (err 5) (fn [x] (* x 2))) ; => err(5)

Since: Phase TC1

defn

result-eq?

(result-eq? [A E])

compare two Result[A B] values.

r1first Result[A B]
r2second Result[A B]
ok-cmpcomparator for ok values fn [a :int b :int] :bool
err-cmpcomparator for err values fn [a :int b :int] :bool

true if both ok with equal ok values, or both err with equal err values.

(result-eq? (ok 1) (ok 1) (fn [a b] (= a b)) (fn [a b] (= a b)))  ; => true

Since: Phase TC1

definstance

Eq[Result]

(definstance Eq [Result])

equality using the ok and err types' Eq instances.

defn

result-bimap

(result-bimap [container :int fn-left fn-right])

map fn-left over the err arm and fn-right over the ok arm.

Follows the Either convention: the err arm is the "left" side and the ok arm is the "right" side. This is the worker behind the Bifunctor [Result] instance (kept here next to the Result layout it depends on).

containerResult[A B]
fn-leftfunction applied to the err value (B -> D)
fn-rightfunction applied to the ok value (A -> C)

A freshly allocated Result[C D]; the input is not modified.

(result-bimap (ok 5) neg inc)   ; => ok(6)
(result-bimap (err 5) neg inc)  ; => err(-5)

Since: stdlib-hkt-consolidation T1

definstance

Bifunctor[Result]

(definstance Bifunctor [Result])

map fn-left over err, fn-right over ok.

(bimap (ok 5) neg inc)   ; => ok(6)
(bimap (err 5) neg inc)  ; => err(-5)

Since: stdlib-hkt-consolidation T1

definstance

Functor[(Result _ B)]

(definstance Functor [(Result _ B)])

map a function over the ok payload; err unchanged.

(fmap (:: (ok 21) (Result int int)) (fn [x] (* x 2)))  ; => ok(42)
(fmap (:: (err 5) (Result int int)) (fn [x] (* x 2)))  ; => err(5)

Since: stdlib-hkt-consolidation T4

definstance

Applicative[(Result _ B)]

(definstance Applicative [(Result _ B)])

pure is ok; ap applies an ok function to an ok argument and returns the first err it meets (the function's before the argument's).

(ap (ok (fn [x] (* x 2))) (ok 21))  ; => ok(42)
(ap (ok (fn [x] (* x 2))) (err 5))  ; => err(5)

Since: typeclass-superclasses-plan SC8b

definstance

Monad[(Result _ B)]

(definstance Monad [(Result _ B)])

bind sequences through ok; short-circuits on err.

(bind (ok 20) (fn [x] (ok (+ x 1))))  ; => ok(21)

Since: stdlib-hkt-consolidation T4

definstance

MonadError[(Result _ B)]

(definstance MonadError [(Result _ B)])

throw-error builds an err; catch-error runs a handler on the err payload (ok values pass through unchanged).

(throw-error 404)                              ; => err(404)
(catch-error (err 5) (fn [e] (ok (* e -1))))   ; => ok(-5)
(catch-error (ok 7) (fn [e] (ok 0)))           ; => ok(7)

Since: stdlib-hkt-consolidation T4

Internal definitions
__tur-q-is-err?-- ? operator helper; checks if a Result value is an error.
__tur-q-ok-val-- ? operator helper; extracts the ok payload from a Result value.