No matching definitions.

tur/result

stdlib/result.tur

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

Since: Phase TC1-E

defstruct

Result

(defstruct Result [A B])

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

Since: Phase TC1

defn

ok

(ok [x :int])

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

xthe ok value (type A)

A heap-allocated Result[A B] with is-ok=true.

(ok 42)  ; => ok(42)

Since: Phase TC1

defn

err

(err [e :int])

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

ethe error value (type B)

A heap-allocated Result[A B] with is-ok=false.

(err 404)  ; => err(404)

Since: Phase TC1

defn

ok?

(ok? [r :int])

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

Since: Phase TC1

defn

err?

(err? [r :int])

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.

(let [r (:: (make-struct Result true 7 "err") (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.

(let [r (:: (make-struct Result false 0 "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 [r :int f])

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

defn

result-eq?

(result-eq? [r1 :int r2 :int ok-cmp err-cmp])

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.

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.