Module Rresult.R
Result value combinators.
Results
type ('a, 'b) t
= ('a, 'b) Result.result
The type for results.
val ok : 'a -> ('a, 'b) Result.result
ok v
isOk v
.
val error : 'b -> ('a, 'b) Result.result
error e
isError e
.
val reword_error : ('b -> 'c) -> ('a, 'b) Result.result -> ('a, 'c) Result.result
reword_error reword r
is:r
ifr = Ok v
Error (reword e)
ifr = Error e
val get_ok : ('a, 'b) Result.result -> 'a
get r
isv
ifr = Ok v
and- raises Invalid_argument
otherwise.
val get_error : ('a, 'b) Result.result -> 'b
get_error r
ise
ifr = Error e
and- raises Invalid_argument
otherwise.
Composing results
val bind : ('a, 'b) Result.result -> ('a -> ('c, 'b) Result.result) -> ('c, 'b) Result.result
bind r f
isf v
ifr = Ok v
andr
ifr = Error _
.
val map : ('a -> 'c) -> ('a, 'b) Result.result -> ('c, 'b) Result.result
map f r
isbind (fun v -> ret (f v))
r.
val join : (('a, 'b) Result.result, 'b) Result.result -> ('a, 'b) Result.result
join r
isv
ifr = Ok v
andr
otherwise.
val (>>=) : ('a, 'b) Result.result -> ('a -> ('c, 'b) Result.result) -> ('c, 'b) Result.result
r >>= f
isbind
r f
.
val (>>|) : ('a, 'b) Result.result -> ('a -> 'c) -> ('c, 'b) Result.result
r >>| f
ismap
r f
.
module Infix : sig ... end
Infix operators.
Error messages
val msg : string -> [> msg ]
msg s
is`Msg s
.
val msgf : ('a, Stdlib.Format.formatter, unit, [> msg ]) Stdlib.format4 -> 'a
msgf fmt ...
formats a message according tofmt
.
val pp_msg : Stdlib.Format.formatter -> msg -> unit
pp_msg ppf m
printsm
onppf
.
val error_msg : string -> ('a, [> msg ]) Result.result
error_msg s
iserror (`Msg s)
.
val error_msgf : ('a, Stdlib.Format.formatter, unit, ('b, [> msg ]) Result.result) Stdlib.format4 -> 'a
error_msgf fmt ...
is an error message formatted according tofmt
.
val reword_error_msg : ?replace:bool -> (string -> msg) -> ('a, msg) Result.result -> ('a, [> msg ]) Result.result
reword_error_msg ~replace reword r
is likereword_error
except ifreplace
isfalse
(default), the result ofreword old_msg
is 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.result
error_to_msg pp_error r
converts errors inr
withpp_error
to an error message.
val error_msg_to_invalid_arg : ('a, msg) Result.result -> 'a
err_msg_to_invalid_arg r
isv
ifr = Ok v
and- raises Invalid_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.
val failwith_error_msg : ('a, msg) Result.result -> 'a
failwith_error_msg r
raisesFailure m
ifr
isError (`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 -> unit
pp_exn_trap ppf bt
printsbt
onppf
.
val trap_exn : ('a -> 'b) -> 'a -> ('b, [> exn_trap ]) Result.result
trap_exn f v
isf 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 inr
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.
Pretty printing
val pp : ok:(Stdlib.Format.formatter -> 'a -> unit) -> error:(Stdlib.Format.formatter -> 'b -> unit) -> Stdlib.Format.formatter -> ('a, 'b) Result.result -> unit
pp ok error ppf r
printsr
onppf
usingok
anderror
according tor
.
val dump : ok:(Stdlib.Format.formatter -> 'a -> unit) -> error:(Stdlib.Format.formatter -> 'b -> unit) -> Stdlib.Format.formatter -> ('a, 'b) Result.result -> unit
dump ~ok ~error
formats an OCaml result value usingok
orerror
according to case, no parentheses are added.
Predicates and comparison
val is_ok : ('a, 'b) Result.result -> bool
is_ok r
istrue
iffr = Ok _
.
val is_error : ('a, 'b) Result.result -> bool
is_error r
istrue
iffr = 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'
testsr
andr'
for equality usingok
anderror
.
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 ordersr
andr'
usingok
anderror
.
Converting
val to_option : ('a, 'b) Result.result -> 'a option
to_option r
isSome v
ifr = Ok v
andNone
otherwise.
val of_option : none:(unit -> ('a, 'b) Result.result) -> 'a option -> ('a, 'b) Result.result
of_option ~none r
isOk v
ifr = Some v
andnone ()
otherwise.
val to_presult : ('a, 'b) Result.result -> [> `Ok of 'a | `Error of 'b ]
to_presult r
isr
as a polymorphic variant result value.
val of_presult : [< `Ok of 'a | `Error of 'b ] -> ('a, 'b) Result.result
of_presult pr
ispr
as 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 -> 'a
ignore_error ~use r
isv
ifr = Ok v
anduse e
ifr = Error e
.
val kignore_error : use:('b -> ('a, 'c) Result.result) -> ('a, 'b) Result.result -> ('a, 'c) Result.result
kignore_error ~use r
isr
ifr = Ok v
anduse e
ifr = Error e
.