package batteries

  1. Overview
  2. Docs
A community-maintained standard library extension

Install

Dune Dependency

Authors

Maintainers

Sources

v3.9.0.tar.gz
md5=ea26b5c72e6731e59d856626049cca4d
sha512=55975b62c26f6db77433a3ac31f97af609fc6789bb62ac38b267249c78fd44ff37fe81901f1cf560857b9493a6046dd37b0d1c0234c66bd59e52843aac3ce6cb

doc/batteries.unthreaded/BatResult/index.html

Module BatResultSource

Monadic results of computations that can raise exceptions

Sourcetype ('a, 'e) t = ('a, 'e) BatPervasives.result =
  1. | Ok of 'a
  2. | Error of 'e

The type of a result. A result is either Ok x carrying the normal return value x or is Error e carrying some indication of an error. The value associated with a bad result is usually an exception (exn) that can be raised.

  • since 1.0
Sourceval ok : 'a -> ('a, 'b) t

ok v is Ok v.

  • since 3.0.0
Sourceval error : 'e -> ('a, 'e) t

error e is Error e.

  • since 3.0.0
Sourceval value : ('a, 'e) t -> default:'a -> 'a

value r ~default is v if r is Ok v and default otherwise.

  • since 3.0.0
Sourceval default : 'a -> ('a, _) t -> 'a

default d r evaluates to d if r is Error else x when r is Ok x.

  • see value

    or a slightly different signature.

  • since 2.0
Sourceval get_ok : ('a, 'e) t -> 'a

get_ok r is v if r is Ok v.

  • since 3.0.0
Sourceval get_error : ('a, 'e) t -> 'e

get_error r is e if r is Error e.

  • since 3.0.0
Sourceval get : ('a, exn) t -> 'a

get (Ok x) returns x, and get (Error e) raises e. This function is, in a way, the opposite of the catch function

  • since 2.0
Sourceval catch : ('a -> 'e) -> 'a -> ('e, exn) t

Execute a function and catch any exception as a result. This function encapsulates code that could throw an exception and returns that exception as a value.

  • since 1.0
Sourceval catch2 : ('a -> 'b -> 'c) -> 'a -> 'b -> ('c, exn) t

As catch but two parameters. This saves a closure construction

  • since 2.0
Sourceval catch3 : ('a -> 'b -> 'c -> 'd) -> 'a -> 'b -> 'c -> ('d, exn) t

As catch but three parameters. This saves a closure construction

  • since 2.0
Sourceval bind : ('a, 'e) t -> ('a -> ('b, 'e) t) -> ('b, 'e) t

bind r f is f v if r is Ok v and r if r is Error _.

  • since 3.0.0
Sourceval join : (('a, 'e) t, 'e) t -> ('a, 'e) t

join rr is r if rr is Ok r and rr if rr is Error _.

  • since 3.0.0
Sourceval map : ('a -> 'b) -> ('a, 'e) t -> ('b, 'e) t

map f r is Ok (f v) if r is Ok v and r if r is Error _.

  • since 3.0.0
Sourceval map_error : ('e -> 'f) -> ('a, 'e) t -> ('a, 'f) t

map_error f r is Error (f e) if r is Error e and r if r is Ok _.

  • since 3.0.0
Sourceval map_both : ('a1 -> 'a2) -> ('b1 -> 'b2) -> ('a1, 'b1) t -> ('a2, 'b2) t

map_both f g (Ok x) returns Ok (f x) and map_both f g (Error e) returns Error (g e).

  • since 2.6.0
Sourceval map_default : 'b -> ('a -> 'b) -> ('a, _) t -> 'b

map_default d f r evaluates to d if r is Error else f x when r is Ok x

  • since 2.0
Sourceval fold : ok:('a -> 'c) -> error:('e -> 'c) -> ('a, 'e) t -> 'c

fold ~ok ~error r is ok v if r is Ok v and error e if r is Error e.

  • since 3.0.0
Sourceval iter : ('a -> unit) -> ('a, 'e) t -> unit

iter f r is f v if r is Ok v and () otherwise.

  • since 3.0.0
Sourceval iter_error : ('e -> unit) -> ('a, 'e) t -> unit

iter_error f r is f e if r is Error e and () otherwise.

  • since 3.0.0

Predicates and comparisons

Sourceval is_ok : ('a, 'e) t -> bool

is_ok (Ok _) is true, otherwise false.

  • since 2.0
Sourceval is_error : ('a, 'e) t -> bool

is_error r is true iff r is Error _.

  • since 3.0.0
Sourceval is_bad : ('a, 'e) t -> bool

Same as is_error.

  • since 2.0
Sourceval is_exn : exn -> ('a, exn) t -> bool

is_exn e1 r is true iff r is Error e2 with e1=e2

Sourceval equal : ok:('a -> 'a -> bool) -> error:('e -> 'e -> bool) -> ('a, 'e) t -> ('a, 'e) t -> bool

equal ~ok ~error r0 r1 tests equality of r0 and r1 using ok and error to respectively compare values wrapped by Ok _ and Error _.

  • since 3.0.0
Sourceval compare : ok:('a -> 'a -> int) -> error:('e -> 'e -> int) -> ('a, 'e) t -> ('a, 'e) t -> int

compare ~ok ~error r0 r1 totally orders r0 and r1 using ok and error to respectively compare values wrapped by Ok _ and Error _. Ok _ values are smaller than Error _ values.

  • since 3.0.0

Converting

Sourceval to_option : ('a, _) t -> 'a option

to_option r is r as an option, mapping Ok v to Some v and Error _ to None.

  • since 1.0
Sourceval of_option : 'a option -> ('a, unit) t

Convert an option to a result

  • since 1.0
Sourceval to_list : ('a, 'e) t -> 'a list

to_list r is [v] if r is Ok v and [] otherwise.

  • since 3.0.0
Sourceval to_seq : ('a, 'e) t -> 'a BatSeq.t

to_seq r is r as a sequence. Ok v is the singleton sequence containing v and Error _ is the empty sequence.

  • since 3.0.0

The Result Monad

This monad is very similar to the option monad, but instead of being None when an error occurs, the first error in the sequence is preserved as the return value.

Sourcemodule Monad : sig ... end

Infix

Sourcemodule Infix : sig ... end

This infix module provides the operator (>>=)

Sourceval print : ('b BatInnerIO.output -> 'a -> unit) -> 'b BatInnerIO.output -> ('a, exn) t -> unit

Print a result as Ok(x) or Error(exn)

OCaml

Innovation. Community. Security.