tur/result
typed Result[A B]: ok value A or err value B.
Since: Phase TC1-E
Result
(defstruct Result [A B])
parameterized result type with ok type A and err type B.
Since: Phase TC1
ok
(ok [A B])
wrap a success value of type A in a Result[A B].
| x | the ok value (type A) |
A Result[A B] with is-ok=true. 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))
Since: Phase TC1 (made polymorphic over A/B as Prereq 3, 2026-06-12;
err
(err [A B])
wrap an error value of type B in a Result[A B].
| e | the error value (type B) |
A Result[A B] with is-ok=false. 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))
Since: Phase TC1 (made polymorphic as Prereq 3, 2026-06-12; same
ok?
(ok? [r : int])
check whether a Result[A B] is ok.
| r | Result[A B] |
true if r is ok, false if r is err.
(ok? (ok 1)) ; => true (ok? (err 0)) ; => false
Since: Phase TC1
err?
(err? [r : int])
check whether a Result[A B] is err.
| r | Result[A B] |
true if r is err, false if r is ok.
(err? (err 1)) ; => true
Since: Phase TC1
ok-val
(ok-val [A B])
extract the ok value from a typed Result[A B].
| r | Result[A B] |
The ok value of type A.
(let [r (:: (make-struct Result true 7 "err") (Result int cstr))]
(ok-val r)) ; => 7
Since: Phase TC1
err-val
(err-val [A B])
extract the error value from a typed Result[A B].
| r | Result[A B] |
The error value of type B.
(let [r (:: (make-struct Result false 0 "boom") (Result int cstr))]
(err-val r)) ; => "boom"
Since: Phase TC1
result-free
(result-free [r : int])
free a heap-allocated Result[A B].
| r | Result[A B] to free |
(result-free my-result)
Since: Phase TC1
result-map
(result-map [A B E])
apply a function to the ok value if ok.
| r | Result[A B] | |
| f | function A -> C |
Result[C B] with f applied to the ok value if ok, or the unchanged err otherwise. The returned value is a freshly allocated Result; the input r is not modified.
(result-map (ok 3) (fn [x] (* x 2))) ; => ok(6) (result-map (err 5) (fn [x] (* x 2))) ; => err(5)
Since: Phase TC1
result-eq?
(result-eq? [r1 : int r2 : int ^fat ok-cmp ^fat err-cmp])
compare two Result[A B] values.
| r1 | first Result[A B] | |
| r2 | second Result[A B] | |
| ok-cmp | comparator for ok values fn [a :int b :int] :bool | |
| err-cmp | comparator 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
Eq[Result]
(definstance Eq [Result])
equality using the ok and err types' Eq instances.
result-bimap
(result-bimap [container : int fn-left fn-right])
map fn-left over the err arm and fn-right over the ok arm.
| container | Result[A B] | |
| fn-left | function applied to the err value (B -> D) | |
| fn-right | function 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
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
Functor[]
(definstance Functor [])
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
Monad[]
(definstance Monad [])
bind sequences through ok; short-circuits on err.
(bind (ok 20) (fn [x] (ok (+ x 1)))) ; => ok(21)
Since: stdlib-hkt-consolidation T4
MonadError[]
(definstance MonadError [])
throw-error builds an err; catch-error runs a
(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.