package base

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Module Base.Or_errorSource

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
Sourcetype 'a t = ('a, Error.t) Result.t

Serialization and comparison of an Error force the error's lazy message.

Sourceval compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
Sourceval equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
Sourceval hash_fold_t : (Hash.state -> 'a -> Hash.state) -> Hash.state -> 'a t -> Hash.state
include Sexplib0.Sexpable.S1 with type 'a t := 'a t
Sourceval t_of_sexp : (Sexplib0.Sexp.t -> 'a) -> Sexplib0.Sexp.t -> 'a t
Sourceval sexp_of_t : ('a -> Sexplib0.Sexp.t) -> 'a t -> Sexplib0.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 Applicative.S_local with type 'a t := 'a t
Sourceval both : 'a t -> 'b t -> ('a * 'b) t
Sourceval (<*>) : ('a -> 'b) t -> 'a t -> 'b t

same as apply

Sourceval (<*) : 'a t -> unit t -> 'a t
Sourceval (*>) : unit t -> 'a t -> 'a t
Sourceval apply : ('a -> 'b) t -> 'a t -> 'b t
Sourceval map2 : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t
Sourceval map3 : 'a t -> 'b t -> 'c t -> f:('a -> 'b -> 'c -> 'd) -> 'd t
Sourcemodule Applicative_infix : sig ... end
include Invariant.S1 with type 'a t := 'a t
Sourceval invariant : ('a -> unit) -> 'a t -> unit
include Monad.S_local with type 'a t := 'a t
Sourceval (>>=) : 'a t -> ('a -> 'b t) -> 'b t

t >>= f returns a computation that sequences the computations represented by two monad elements. The resulting computation first does t to yield a value v, and then runs the computation returned by f v.

Sourceval (>>|) : 'a t -> ('a -> 'b) -> 'b t

t >>| f is t >>= (fun a -> return (f a)).

Sourcemodule Monad_infix : sig ... end
Sourceval bind : 'a t -> f:('a -> 'b t) -> 'b t

bind t ~f = t >>= f

Sourceval return : 'a -> 'a t

return v returns the (trivial) computation that returns v.

Sourceval join : 'a t t -> 'a t

join t is t >>= (fun t' -> t').

Sourceval ignore_m : 'a t -> unit t

ignore_m t is map t ~f:(fun _ -> ()). ignore_m used to be called ignore, but we decided that was a bad name, because it shadowed the widely used Stdlib.ignore. Some monads still do let ignore = ignore_m for historical reasons.

Sourceval all : 'a t list -> 'a list t
Sourceval all_unit : unit t list -> unit t

Like all, but ensures that every monadic value in the list produces a unit value, all of which are discarded rather than being collected into a list.

Sourcemodule Let_syntax : sig ... end

These are convenient to have in scope when programming with a monad:

Sourceval is_ok : _ t -> bool
Sourceval is_error : _ t -> bool
Sourceval 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).

Sourceval try_with_join : ?backtrace:bool -> (unit -> 'a t) -> 'a t
Sourceval ok : 'ok t -> 'ok option

ok t returns None if t is an Error, and otherwise returns the contents of the Ok constructor.

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

Sourceval of_exn : ?backtrace:[ `Get | `This of string ] -> exn -> _ t

of_exn ?backtrace exn is Error (Error.of_exn ?backtrace exn).

Sourceval of_exn_result : ?backtrace:[ `Get | `This of string ] -> ('a, exn) Result.t -> 'a t

of_exn_result ?backtrace (Ok a) = Ok a

of_exn_result ?backtrace (Error exn) = of_exn ?backtrace exn

Sourceval error : ?here:Lexing.position -> ?strict:unit -> string -> 'a -> ('a -> 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.

Sourceval error_s : Sexp.t -> _ t
Sourceval error_string : string -> _ t

error_string message is Error (Error.of_string message).

Sourceval errorf : ('a, unit, string, _ t) 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.

Sourceval tag : 'a t -> tag:string -> 'a t

tag t ~tag is Result.map_error t ~f:(Error.tag ~tag).

Sourceval tag_s : 'a t -> tag:Sexp.t -> 'a t

tag_s is like tag with a sexp tag.

Sourceval tag_s_lazy : 'a t -> tag:Sexp.t Lazy.t -> 'a t

tag_s_lazy is like tag with a lazy sexp tag.

Sourceval tag_arg : 'a t -> string -> 'b -> ('b -> Sexp.t) -> 'a t

tag_arg is like tag, with a tag that has a sexpable argument.

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

Sourceval map : 'a t -> f:('a -> 'b) -> 'b t
Sourceval iter : 'a t -> f:('a -> unit) -> unit
Sourceval iter_error : _ t -> f:(Error.t -> unit) -> unit
Sourceval combine_errors : 'a t list -> 'a list t

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]) 
Sourceval combine_errors_unit : unit t list -> unit t

combine_errors_unit ts returns Ok if every element in ts is Ok (), else it returns Error with all the errors in ts, like combine_errors.

Sourceval filter_ok_at_least_one : 'a t list -> 'a list t

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.

Sourceval find_ok : 'a t list -> 'a t

find_ok ts returns the first value in ts that is Ok, otherwise it returns the same error as combine_errors ts.

Sourceval find_map_ok : 'a list -> f:('a -> 'b t) -> 'b t

find_map_ok l ~f returns the first value in l for which f returns Ok, otherwise it returns the same error as combine_errors (List.map l ~f).

OCaml

Innovation. Community. Security.