Type for tracking errors in an Error.t. This is a specialization of the Result type, where the Error constructor carries an Error.t.
A common idiom is to wrap a function that is not implemented on all platforms, e.g.:
val do_something_linux_specific : (unit -> unit) Or_error.t
include sig ... end
val hash_fold_t : (Base.Hash.state ‑> 'a ‑> Base.Hash.state) ‑> Base.Hash.state ‑> 'a t ‑> Base.Hash.state
val t_of_sexp : (Base.Sexp.t ‑> 'a) ‑> Base.Sexp.t ‑> 'a t
val sexp_of_t : ('a ‑> Base.Sexp.t) ‑> 'a t ‑> Base.Sexp.t
Applicative
functions don't have quite the same semantics as
Applicative.of_Monad(Or_error)
would give -- apply (Error e1) (Error e2)
returns
the combination of e1
and e2
, whereas it would only return e1
if it were defined
using bind
.
include Base.Applicative.S with type a t := a t
val return : 'a ‑> 'a t
module Applicative_infix : sig ... end
include Base.Invariant.S1 with type a t := a t
val invariant : 'a Base__.Invariant_intf.inv ‑> 'a t Base__.Invariant_intf.inv
include Base.Monad.S with type a t := a t
include Base__.Monad_intf.S_without_syntax with type a t := a t
type 'a t
A monad is an abstraction of the concept of sequencing of computations. A value of type 'a monad represents a computation that returns a value of type 'a.
include Base__.Monad_intf.Infix with type a t := a t
module Monad_infix : Base__.Monad_intf.Infix with type a t := a t
module Ok : Base.Container.S1 with type a t := a t
val is_ok : _ t ‑> bool
val is_error : _ t ‑> bool
val try_with : ?backtrace:bool ‑> (unit ‑> 'a) ‑> 'a t
try_with f
catches exceptions thrown by f
and returns them in the Result.t as an
Error.t. try_with_join
is like try_with
, except that f
can throw exceptions or
return an Error directly, without ending up with a nested error; it is equivalent to
Result.join (try_with f)
.
val ok : 'ok t ‑> 'ok option
ok t
returns None
if t
is an Error
, and otherwise returns the
contents of the Ok
constructor.
val ok_exn : 'a t ‑> 'a
ok_exn t
throws an exception if t
is an Error
, and otherwise returns the
contents of the Ok
constructor.
val of_exn : ?backtrace:[ `Get | `This of string ] ‑> exn ‑> _ t
of_exn exn
is Error (Error.of_exn exn)
.
val of_exn_result : ('a, exn) Base.Result.t ‑> 'a t
of_exn_result (Ok a) = Ok a
, of_exn_result (Error exn) = of_exn exn
val error : ?strict:unit ‑> string ‑> 'a ‑> ('a ‑> Base.Sexp.t) ‑> _ t
error
is a wrapper around Error.create
:
error ?strict message a sexp_of_a
= Error (Error.create ?strict message a sexp_of_a)
As with Error.create
, sexp_of_a a
is lazily computed, when the info is converted
to a sexp. So, if a
is mutated in the time between the call to create
and the
sexp conversion, those mutations will be reflected in the sexp. Use ~strict:()
to
force sexp_of_a a
to be computed immediately.
val error_s : Base.Sexp.t ‑> _ t
val errorf : ('a, unit, string, _ t) Pervasives.format4 ‑> 'a
errorf format arg1 arg2 ...
is Error (sprintf format arg1 arg2 ...)
. Note that it
calculates the string eagerly, so when performance matters you may want to use error
instead.
val tag_arg : 'a t ‑> string ‑> 'b ‑> ('b ‑> Base.Sexp.t) ‑> 'a t
val unimplemented : string ‑> _ t
For marking a given value as unimplemented. Typically combined with conditional compilation, where on some platforms the function is defined normally, and on some platforms it is defined as unimplemented. The supplied string should be the name of the function that is unimplemented.
val iter : 'a t ‑> f:('a ‑> unit) ‑> unit
val iter_error : _ t ‑> f:(Base.Error.t ‑> unit) ‑> unit
combine_errors ts
returns Ok
if every element in ts
is Ok
, else it returns
Error
with all the errors in ts
. More precisely:
combine_errors [Ok a1; ...; Ok an] = Ok [a1; ...; an]
combine_errors [...; Error e1; ...; Error en; ...]
= Error (Error.of_list [e1; ...; en])
combine_errors_unit
returns Ok
if every element in ts
is Ok ()
, else it
returns Error
with all the errors in ts
, like combine_errors
.
filter_ok_at_least_one ts
returns all values in ts
that are Ok
if there is at
least one, otherwise it returns the same error as combine_errors ts
.