Module Base.Result

Result is often used to handle error messages.

type ('ok, 'err) t = ('ok'err) Pervasives.result =
| Ok of 'ok
| Error of 'err

'ok is a function's expected return type, and 'err is often an error message string.

      let ric_of_ticker = function
        | "IBM" -> Ok "IBM.N"
        | "MSFT" -> Ok "MSFT.OQ"
        | "AA" -> Ok "AA.N"
        | "CSCO" -> Ok "CSCO.OQ"
        | _ as ticker -> Error (sprintf "can't find ric of %s" ticker)

The return type of ric_of_ticker could be string option, but (string, string) Result.t gives more control over the error message.

include sig ... end
val t_of_sexp : (Sexp.t ‑> 'ok) ‑> (Sexp.t ‑> 'err) ‑> Sexp.t ‑> ('ok'errt
val sexp_of_t : ('ok ‑> Sexp.t) ‑> ('err ‑> Sexp.t) ‑> ('ok'errt ‑> Sexp.t
val compare : ('ok ‑> 'ok ‑> int) ‑> ('err ‑> 'err ‑> int) ‑> ('ok'errt ‑> ('ok'errt ‑> int
val hash_fold_t : (Hash.state ‑> 'ok ‑> Hash.state) ‑> (Hash.state ‑> 'err ‑> Hash.state) ‑> Hash.state ‑> ('ok'errt ‑> Hash.state
include Monad.S2 with type (a, err) t := (a, err) t
type ('a, 'e) t
include Base__.Monad_intf.Infix2 with type (a, e) t := (a, e) t
type ('a, 'e) t
val (>>=) : ('a'et ‑> ('a ‑> ('b'et) ‑> ('b'et
val (>>|) : ('a'et ‑> ('a ‑> 'b) ‑> ('b'et
include Base__.Monad_intf.Syntax2 with type (a, e) t := (a, e) t
type ('a, 'e) t
module Let_syntax : sig ... end
module Monad_infix : Base__.Monad_intf.Infix2 with type (a, e) t := (a, e) t
val bind : ('a'et ‑> f:('a ‑> ('b'et) ‑> ('b'et
val return : 'a ‑> ('a_t
val map : ('a'et ‑> f:('a ‑> 'b) ‑> ('b'et
val join : (('a'et'et ‑> ('a'et
val ignore_m : (_'et ‑> (unit, 'et
val all : ('a'et list ‑> ('a list, 'et
val all_unit : (unit, 'et list ‑> (unit, 'et
val all_ignore : (unit, 'et list ‑> (unit, 'et
  • Deprecated [since 2018-02] Use [all_unit]
val ignore : (_'errt ‑> (unit, 'errt
val fail : 'err ‑> (_'errt
val failf : ('a, unit, string, (_, string) t) Pervasives.format4 ‑> 'a

e.g., failf "Couldn't find bloogle %s" (Bloogle.to_string b).

val is_ok : (__t ‑> bool
val is_error : (__t ‑> bool
val ok : ('ok_t ‑> 'ok option
val ok_exn : ('ok, exn) t ‑> 'ok
val ok_or_failwith : ('ok, string) t ‑> 'ok
val error : (_'errt ‑> 'err option
val of_option : 'ok option ‑> error:'err ‑> ('ok'errt
val iter : ('ok_t ‑> f:('ok ‑> unit) ‑> unit
val iter_error : (_'errt ‑> f:('err ‑> unit) ‑> unit
val map : ('ok'errt ‑> f:('ok ‑> 'c) ‑> ('c'errt
val map_error : ('ok'errt ‑> f:('err ‑> 'c) ‑> ('ok'ct
val combine : ('ok1'errt ‑> ('ok2'errt ‑> ok:('ok1 ‑> 'ok2 ‑> 'ok3) ‑> err:('err ‑> 'err ‑> 'err) ‑> ('ok3'errt

Returns Ok if both are Ok and Error otherwise.

val combine_errors : ('ok'errt list ‑> ('ok list, 'err list) t

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

This is similar to all from Monad.S2, with the difference that all only returns the first error.

val combine_errors_unit : (unit, 'errt list ‑> (unit, 'err list) t

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.

val ok_fst : ('ok'errt ‑> [ `Fst of 'ok | `Snd of 'err ]

ok_fst is useful with List.partition_map. Continuing the above example:

      let rics, errors = List.partition_map ~f:Result.ok_fst
                           (List.map ~f:ric_of_ticker ["AA"; "F"; "CSCO"; "AAPL"])
val ok_if_true : bool ‑> error:'err ‑> (unit, 'errt

ok_if_true returns Ok () if bool is true, and Error error if it is false.

val try_with : (unit ‑> 'a) ‑> ('a, exn) t
val ok_unit : (unit, _t

ok_unit = Ok (), used to avoid allocation as a performance hack.

module Export : sig ... end