Module CCResult
Error Monad
Uses the new "result" type from OCaml 4.03.
- since
- 0.16
type 'a equal
= 'a -> 'a -> bool
type 'a ord
= 'a -> 'a -> int
type 'a printer
= Stdlib.Format.formatter -> 'a -> unit
Basics
type nonrec (+'good, +'bad) result
= ('good, 'bad) Stdlib.result
=
|
Ok of 'good
|
Error of 'bad
type (+'good, +'bad) t
= ('good, 'bad) result
=
|
Ok of 'good
|
Error of 'bad
val return : 'a -> ('a, 'err) t
Successfully return a value.
val fail : 'err -> ('a, 'err) t
Fail with an error.
val of_exn : exn -> ('a, string) t
of_exn e
usesPrintexc
to print the exception as a string.
val of_exn_trace : exn -> ('a, string) t
of_exn_trace e
is similar toof_exn e
, but it adds the stacktrace to the error message.Remember to call
Printexc.record_backtrace true
and compile with the debug flag for this to work.
val fail_printf : ('a, Stdlib.Buffer.t, unit, ('b, string) t) Stdlib.format4 -> 'a
fail_printf format
usesformat
to obtain an error message and then returnsError msg
.
val fail_fprintf : ('a, Stdlib.Format.formatter, unit, ('b, string) t) Stdlib.format4 -> 'a
fail_fprintf format
usesformat
to obtain an error message and then returnsError msg
.
val add_ctx : string -> ('a, string) t -> ('a, string) t
add_ctx msg
leavesOk x
untouched, but transformsError s
intoError s'
wheres'
contains the additional context given bymsg
.- since
- 1.2
val add_ctxf : ('a, Stdlib.Format.formatter, unit, ('b, string) t -> ('b, string) t) Stdlib.format4 -> 'a
add_ctxf format_message
is similar toadd_ctx
but withFormat
for printing the message (eagerly). Example:add_ctxf "message(number %d, foo: %B)" 42 true (Error "error)"
- since
- 1.2
val map2 : ('a -> 'b) -> ('err1 -> 'err2) -> ('a, 'err1) t -> ('b, 'err2) t
Like
map
, but also with a function that can transform the error message in case of failure.
val iter : ('a -> unit) -> ('a, _) t -> unit
Apply the function only in case of
Ok
.
val iter_err : ('err -> unit) -> (_, 'err) t -> unit
Apply the function in case of
Error
.- since
- 2.4
val get_exn : ('a, _) t -> 'a
Extract the value
x
fromOk x
, fails otherwise. You should be careful with this function, and favor other combinators whenever possible.- raises Get_error
if the value is an error.
val get_or : ('a, _) t -> default:'a -> 'a
get_or e ~default
returnsx
ife = Ok x
,default
otherwise.
val get_lazy : ('e -> 'a) -> ('a, 'e) t -> 'a
get_lazy f e
returnsx
ife = Ok x
,f msg
ife = Error msg
. This is similar toCCOpt.get_lazy
.- since
- 3.0
val get_or_failwith : ('a, string) t -> 'a
get_or_failwith e
returnsx
ife = Ok x
, fails otherwise.- raises Failure
with
msg
ife = Error msg
.
- since
- 2.4
val get_lazy : ('b -> 'a) -> ('a, 'b) t -> 'a
get_lazy default_fn x
unwrapsx
, but ifx = Error e
it returnsdefault_fr e
instead.- since
- 3.0
val map_or : ('a -> 'b) -> ('a, 'c) t -> default:'b -> 'b
map_or f e ~default
returnsf x
ife = Ok x
,default
otherwise.
val catch : ('a, 'err) t -> ok:('a -> 'b) -> err:('err -> 'b) -> 'b
catch e ~ok ~err
calls eitherok
orerr
depending on the value ofe
.
val flat_map : ('a -> ('b, 'err) t) -> ('a, 'err) t -> ('b, 'err) t
val equal : err:'err equal -> 'a equal -> ('a, 'err) t equal
val compare : err:'err ord -> 'a ord -> ('a, 'err) t ord
val fold : ok:('a -> 'b) -> error:('err -> 'b) -> ('a, 'err) t -> 'b
fold ~ok ~error e
opense
and, ife = Ok x
, returnsok x
, otherwisee = Error s
and it returnserror s
.
val fold_ok : ('a -> 'b -> 'a) -> 'a -> ('b, _) t -> 'a
fold_ok f acc r
will computef acc x
ifr=Ok x
, and returnacc
otherwise, as if the result were a mere option.- since
- 1.2
val is_ok : ('a, 'err) t -> bool
Return true if
Ok
.- since
- 1.0
val is_error : ('a, 'err) t -> bool
Return true if
Error
.- since
- 1.0
Wrappers
val guard : (unit -> 'a) -> ('a, exn) t
guard f
runsf ()
and returns its result wrapped inOk
. Iff ()
raises some exceptione
, then it fails withError e
.
val guard_str_trace : (unit -> 'a) -> ('a, string) t
Like
guard_str
but usesof_exn_trace
instead ofof_exn
so that the stack trace is printed.
Applicative
Infix
module Infix : sig ... end
include module type of Infix
val (>>=) : ('a, 'err) t -> ('a -> ('b, 'err) t) -> ('b, 'err) t
Monadic composition.
e >>= f
proceeds asf x
ife
isOk x
or returnse
ife
is anError
.
val (<*>) : ('a -> 'b, 'err) t -> ('a, 'err) t -> ('b, 'err) t
a <*> b
evaluatesa
andb
, and, in case of success, returnsOk (a b)
. Otherwise, it fails, and the error ofa
is chosen over the error ofb
if both fail.
Let operators on OCaml >= 4.08.0, nothing otherwise
- since
- 2.8
include CCShimsMkLet_.S2 with type ('a, 'e) t_let2 := ('a, 'e) result
Let operators on OCaml >= 4.08.0, nothing otherwise
- since
- 2.8
include CCShimsMkLet_.S2 with type ('a, 'e) t_let2 := ('a, 'e) result
Collections
val flatten_l : ('a, 'err) t list -> ('a list, 'err) t
Same as
map_l id
: returnsOk [x1;…;xn]
ifl=[Ok x1; …; Ok xn]
, or the first error otherwise.- since
- 2.7
Misc
val choose : ('a, 'err) t list -> ('a, 'err list) t
choose l
selects a member ofl
that is aOk _
value, or returnsError l
otherwise, wherel
is the list of errors.
val retry : int -> (unit -> ('a, 'err) t) -> ('a, 'err list) t
retry n f
callsf
at mostn
times, returning the first result off ()
that doesn't fail. Iff
failsn
times,retry n f
fails with the list of successive errors.
module type MONAD = sig ... end
Conversions
val to_opt : ('a, _) t -> 'a option
Convert a result to an option.
val of_opt : 'a option -> ('a, string) t
Convert an option to a result.
val to_seq : ('a, _) t -> 'a Stdlib.Seq.t
Renamed from
to_std_seq
since 3.0.- since
- 3.0