Module Rresult.R
Result value combinators.
Results
type ('a, 'b) t= ('a, 'b) Result.resultThe type for results.
val ok : 'a -> ('a, 'b) Result.resultok visOk v.
val error : 'b -> ('a, 'b) Result.resulterror eisError e.
val reword_error : ('b -> 'c) -> ('a, 'b) Result.result -> ('a, 'c) Result.resultreword_error reword ris:rifr = Ok vError (reword e)ifr = Error e
val get_ok : ('a, 'b) Result.result -> 'aget risvifr = Ok vand- raises Invalid_argument
otherwise.
val get_error : ('a, 'b) Result.result -> 'bget_error riseifr = Error eand- raises Invalid_argument
otherwise.
Composing results
val bind : ('a, 'b) Result.result -> ('a -> ('c, 'b) Result.result) -> ('c, 'b) Result.resultbind r fisf vifr = Ok vandrifr = Error _.
val map : ('a -> 'c) -> ('a, 'b) Result.result -> ('c, 'b) Result.resultmap f risbind (fun v -> ret (f v))r.
val join : (('a, 'b) Result.result, 'b) Result.result -> ('a, 'b) Result.resultjoin risvifr = Ok vandrotherwise.
val (>>=) : ('a, 'b) Result.result -> ('a -> ('c, 'b) Result.result) -> ('c, 'b) Result.resultr >>= fisbindr f.
val (>>|) : ('a, 'b) Result.result -> ('a -> 'c) -> ('c, 'b) Result.resultr >>| fismapr f.
module Infix : sig ... endInfix operators.
Error messages
val msg : string -> [> msg ]msg sis`Msg s.
val msgf : ('a, Stdlib.Format.formatter, unit, [> msg ]) Stdlib.format4 -> 'amsgf fmt ...formats a message according tofmt.
val pp_msg : Stdlib.Format.formatter -> msg -> unitpp_msg ppf mprintsmonppf.
val error_msg : string -> ('a, [> msg ]) Result.resulterror_msg siserror (`Msg s).
val error_msgf : ('a, Stdlib.Format.formatter, unit, ('b, [> msg ]) Result.result) Stdlib.format4 -> 'aerror_msgf fmt ...is an error message formatted according tofmt.
val reword_error_msg : ?replace:bool -> (string -> msg) -> ('a, msg) Result.result -> ('a, [> msg ]) Result.resultreword_error_msg ~replace reword ris likereword_errorexcept ifreplaceisfalse(default), the result ofreword old_msgis concatened, on a new line to the old message.
val error_to_msg : pp_error:(Stdlib.Format.formatter -> 'b -> unit) -> ('a, 'b) Result.result -> ('a, [> msg ]) Result.resulterror_to_msg pp_error rconverts errors inrwithpp_errorto an error message.
val error_msg_to_invalid_arg : ('a, msg) Result.result -> 'aerr_msg_to_invalid_arg risvifr = Ok vand- raises Invalid_argument
with the error message otherwise.
val open_error_msg : ('a, msg) Result.result -> ('a, [> msg ]) Result.resultopen_error_msg rallows to combine a closed error message variant with other variants.
val failwith_error_msg : ('a, msg) Result.result -> 'afailwith_error_msg rraisesFailure mifrisError (`Msg m).
Trapping unexpected exceptions
Getting rid of null was not enough.
type exn_trap=[|`Exn_trap of exn * Stdlib.Printexc.raw_backtrace]The type for exception traps.
val pp_exn_trap : Stdlib.Format.formatter -> exn_trap -> unitpp_exn_trap ppf btprintsbtonppf.
val trap_exn : ('a -> 'b) -> 'a -> ('b, [> exn_trap ]) Result.resulttrap_exn f visf vand traps any exception that may occur as an exception trap error.
val error_exn_trap_to_msg : ('a, exn_trap) Result.result -> ('a, [> msg ]) Result.resulterror_exn_trap_to_msg rconverts exception trap errors inrto an error message.
val open_error_exn_trap : ('a, exn_trap) Result.result -> ('a, [> exn_trap ]) Result.resultopen_error_exn_trap rallows to combine a closed exception trap error variant with other variants.
Pretty printing
val pp : ok:(Stdlib.Format.formatter -> 'a -> unit) -> error:(Stdlib.Format.formatter -> 'b -> unit) -> Stdlib.Format.formatter -> ('a, 'b) Result.result -> unitpp ok error ppf rprintsronppfusingokanderroraccording tor.
val dump : ok:(Stdlib.Format.formatter -> 'a -> unit) -> error:(Stdlib.Format.formatter -> 'b -> unit) -> Stdlib.Format.formatter -> ('a, 'b) Result.result -> unitdump ~ok ~errorformats an OCaml result value usingokorerroraccording to case, no parentheses are added.
Predicates and comparison
val is_ok : ('a, 'b) Result.result -> boolis_ok ristrueiffr = Ok _.
val is_error : ('a, 'b) Result.result -> boolis_error ristrueiffr = Error _.
val equal : ok:('a -> 'a -> bool) -> error:('b -> 'b -> bool) -> ('a, 'b) Result.result -> ('a, 'b) Result.result -> boolequal ~ok ~error r r'testsrandr'for equality usingokanderror.
val compare : ok:('a -> 'a -> int) -> error:('b -> 'b -> int) -> ('a, 'b) Result.result -> ('a, 'b) Result.result -> intcompare ~ok ~error r r'totally ordersrandr'usingokanderror.
Converting
val to_option : ('a, 'b) Result.result -> 'a optionto_option risSome vifr = Ok vandNoneotherwise.
val of_option : none:(unit -> ('a, 'b) Result.result) -> 'a option -> ('a, 'b) Result.resultof_option ~none risOk vifr = Some vandnone ()otherwise.
val to_presult : ('a, 'b) Result.result -> [> `Ok of 'a | `Error of 'b ]to_presult risras a polymorphic variant result value.
val of_presult : [< `Ok of 'a | `Error of 'b ] -> ('a, 'b) Result.resultof_presult prispras a result value.
Ignoring errors
Warning. Using these functions is, most of the time, a bad idea.
val ignore_error : use:('b -> 'a) -> ('a, 'b) Result.result -> 'aignore_error ~use risvifr = Ok vanduse eifr = Error e.
val kignore_error : use:('b -> ('a, 'c) Result.result) -> ('a, 'b) Result.result -> ('a, 'c) Result.resultkignore_error ~use risrifr = Ok vanduse eifr = Error e.