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 [x :int])
wrap a success value of type A in a Result[A B].
| x | the ok value (type A) |
A heap-allocated Result[A B] with is-ok=true.
(ok 42) ; => ok(42)
Since: Phase TC1
err
(err [e :int])
wrap an error value of type B in a Result[A B].
| e | the error value (type B) |
A heap-allocated Result[A B] with is-ok=false.
(err 404) ; => err(404)
Since: Phase TC1
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 [r :int f])
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 ok-cmp 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.
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.