module R:sig
..end
type('a, 'b)
t =('a, 'b) Result.result
val ok : 'a -> ('a, 'b) Result.result
ok v
is Ok v
.val error : 'b -> ('a, 'b) Result.result
error e
is Error e
.val reword_error : ('b -> 'c) -> ('a, 'b) Result.result -> ('a, 'c) Result.result
reword_error reword r
is:
r
if r = Ok v
Error (reword e)
if r = Error e
val get_ok : ('a, 'b) Result.result -> 'a
get r
is v
if r = Ok v
andInvalid_argument
otherwise.val get_error : ('a, 'b) Result.result -> 'b
get_error r
is e
if r = Error e
andInvalid_argument
otherwise.val pp : pp_ok:(Format.formatter -> 'a -> unit) ->
pp_error:(Format.formatter -> 'b -> unit) ->
Format.formatter -> ('a, 'b) Result.result -> unit
pp pp_ok pp_error ppf r
prints r
on ppf
using pp_ok
and
pp_error
.val bind : ('a, 'b) Result.result ->
('a -> ('c, 'b) Result.result) -> ('c, 'b) Result.result
bind r f
is f v
if r = Ok v
and r
if r = Error _
.val map : ('a -> 'c) -> ('a, 'b) Result.result -> ('c, 'b) Result.result
map f r
is bind (fun v -> ret (f v))
r.val join : (('a, 'b) Result.result, 'b) Result.result -> ('a, 'b) Result.result
join r
is v
if r = Ok v
and r
otherwise.val (>>=) : ('a, 'b) Result.result ->
('a -> ('c, 'b) Result.result) -> ('c, 'b) Result.result
val (>>|) : ('a, 'b) Result.result -> ('a -> 'c) -> ('c, 'b) Result.result
module Infix:sig
..end
typemsg =
[ `Msg of string ]
val msg : string -> [> msg ]
msg s
is `Msg s
.val msgf : ('a, Format.formatter, unit, [> msg ]) Pervasives.format4 -> 'a
msgf fmt ...
formats a message according to fmt
.val pp_msg : Format.formatter -> msg -> unit
pp_msg ppf m
prints m
on ppf
.val error_msg : string -> ('a, [> msg ]) Result.result
error_msg s
is error (`Msg s)
.val error_msgf : ('a, Format.formatter, unit, ('b, [> msg ]) Result.result)
Pervasives.format4 -> 'a
error_msgf fmt ...
is an error message formatted according to fmt
.val reword_error_msg : ?replace:bool ->
(string -> msg) ->
('a, msg) Result.result -> ('a, [> msg ]) Result.result
reword_error_msg ~replace reword r
is like Rresult.R.reword_error
except
if replace
is false
(default), the result of reword old_msg
is
concatened, on a new line to the old message.val error_to_msg : pp_error:(Format.formatter -> 'b -> unit) ->
('a, 'b) Result.result -> ('a, [> msg ]) Result.result
error_to_msg pp_error r
converts errors in r
with pp_error
to
an error message.val error_msg_to_invalid_arg : ('a, msg) Result.result -> 'a
err_msg_to_invalid_arg r
is v
if r = Ok v
andInvalid_argument
with the error message otherwise.val open_error_msg : ('a, msg) Result.result -> ('a, [> msg ]) Result.result
open_error_msg r
allows to combine a closed error message
variant with other variants.
Getting rid of null
was not enough.
typeexn_trap =
[ `Exn_trap of exn * Printexc.raw_backtrace ]
val pp_exn_trap : Format.formatter -> exn_trap -> unit
pp_exn_trap ppf bt
prints bt
on ppf
.val trap_exn : ('a -> 'b) -> 'a -> ('b, [> exn_trap ]) Result.result
trap_exn f v
is f v
and 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.result
error_exn_trap_to_msg r
converts exception trap errors in
r
to an error message.val open_error_exn_trap : ('a, exn_trap) Result.result ->
('a, [> exn_trap ]) Result.result
open_error_exn_trap r
allows to combine a closed exception trap error
variant with other variants.val is_ok : ('a, 'b) Result.result -> bool
is_ok r
is true
iff r = Ok _
.val is_error : ('a, 'b) Result.result -> bool
is_error r
is true
iff r = Error _
.val equal : ok:('a -> 'a -> bool) ->
error:('b -> 'b -> bool) ->
('a, 'b) Result.result -> ('a, 'b) Result.result -> bool
equal ~ok ~error r r'
tests r
and r'
for equality using ok
and error
.val compare : ok:('a -> 'a -> int) ->
error:('b -> 'b -> int) ->
('a, 'b) Result.result -> ('a, 'b) Result.result -> int
compare ~ok ~error r r'
totally orders r
and r'
using ok
and error
.val to_option : ('a, 'b) Result.result -> 'a option
to_option r
is Some v
if r = Ok v
and None
otherwise.val of_option : none:(unit -> ('a, 'b) Result.result) -> 'a option -> ('a, 'b) Result.result
of_option ~none r
is Ok v
if r = Some v
and none ()
otherwise.val to_presult : ('a, 'b) Result.result -> [> `Error of 'b | `Ok of 'a ]
to_presult r
is r
as a polymorphic variant result value.val of_presult : [< `Error of 'b | `Ok of 'a ] -> ('a, 'b) Result.result
of_presult pr
is pr
as a result value.
Warning. Using these functions is, most of the time, a bad idea.
val ignore_error : use:'a -> ('a, 'b) Result.result -> 'a
ignore_error ~use r
is v
if r = Ok v
and use
otherwise.val kignore_error : use:('a, 'c) Result.result ->
('a, 'b) Result.result -> ('a, 'c) Result.result
kignore_error ~use r
is r
if r = Ok v
and use
otherwise.