Module CCResult
Error Monad
Uses the new "result" type from OCaml 4.03.
- since
- 0.16
type 'a equal= 'a -> 'a -> booltype 'a ord= 'a -> 'a -> inttype 'a printer= Format.formatter -> 'a -> unit
Basics
type nonrec (+'good, +'bad) result= ('good, 'bad) Pervasives.result=|Ok of 'good|Error of 'badtype (+'good, +'bad) t= ('good, 'bad) result=|Ok of 'good|Error of 'bad
val return : 'a -> ('a, 'err) tSuccessfully return a value.
val fail : 'err -> ('a, 'err) tFail with an error.
val of_exn : exn -> ('a, string) tof_exn eusesPrintexcto print the exception as a string.
val of_exn_trace : exn -> ('a, string) tof_exn_trace eis similar toof_exn e, but it adds the stacktrace to the error message.Remember to call
Printexc.record_backtrace trueand compile with the debug flag for this to work.
val fail_printf : ('a, Buffer.t, unit, ('b, string) t) Pervasives.format4 -> 'afail_printf formatusesformatto obtain an error message and then returnsError msg.
val fail_fprintf : ('a, Format.formatter, unit, ('b, string) t) Pervasives.format4 -> 'afail_fprintf formatusesformatto obtain an error message and then returnsError msg.
val add_ctx : string -> ('a, string) t -> ('a, string) tadd_ctx msgleavesOk xuntouched, but transformsError sintoError s'wheres'contains the additional context given bymsg.- since
- 1.2
val add_ctxf : ('a, Format.formatter, unit, ('b, string) t -> ('b, string) t) Pervasives.format4 -> 'aadd_ctxf format_messageis similar toadd_ctxbut withFormatfor 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) tLike
map, but also with a function that can transform the error message in case of failure.
val iter : ('a -> unit) -> ('a, _) t -> unitApply the function only in case of
Ok.
val iter_err : ('err -> unit) -> (_, 'err) t -> unitApply the function in case of
Error.- since
- 2.4
val get_exn : ('a, _) t -> 'aExtract the value
xfromOk 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 -> 'aget_or e ~defaultreturnsxife = Ok x,defaultotherwise.
val get_or_failwith : ('a, string) t -> 'aget_or_failwith ereturnsxife = Ok x, fails otherwise.- raises Failure
with
msgife = Error msg.
- since
- 2.4
val map_or : ('a -> 'b) -> ('a, 'c) t -> default:'b -> 'bmap_or f e ~defaultreturnsf xife = Ok x,defaultotherwise.
val catch : ('a, 'err) t -> ok:('a -> 'b) -> err:('err -> 'b) -> 'bcatch e ~ok ~errcalls eitherokorerrdepending on the value ofe.
val flat_map : ('a -> ('b, 'err) t) -> ('a, 'err) t -> ('b, 'err) tval (>|=) : ('a, 'err) t -> ('a -> 'b) -> ('b, 'err) tval (>>=) : ('a, 'err) t -> ('a -> ('b, 'err) t) -> ('b, 'err) tMonadic composition.
e >>= fproceeds asf xifeisOk xor returnseifeis anError.
val equal : err:'err equal -> 'a equal -> ('a, 'err) t equalval compare : err:'err ord -> 'a ord -> ('a, 'err) t ordval fold : ok:('a -> 'b) -> error:('err -> 'b) -> ('a, 'err) t -> 'bfold ~ok ~error eopenseand, ife = Ok x, returnsok x, otherwisee = Error sand it returnserror s.
val fold_ok : ('a -> 'b -> 'a) -> 'a -> ('b, _) t -> 'afold_ok f acc rwill computef acc xifr=Ok x, and returnaccotherwise, as if the result were a mere option.- since
- 1.2
val is_ok : ('a, 'err) t -> boolReturn true if
Ok.- since
- 1.0
val is_error : ('a, 'err) t -> boolReturn true if
Error.- since
- 1.0
Wrappers
val guard : (unit -> 'a) -> ('a, exn) tguard frunsf ()and returns its result wrapped inOk. Iff ()raises some exceptione, then it fails withError e.
val guard_str_trace : (unit -> 'a) -> ('a, string) tLike
guard_strbut usesof_exn_traceinstead ofof_exnso that the stack trace is printed.
Applicative
val (<*>) : ('a -> 'b, 'err) t -> ('a, 'err) t -> ('b, 'err) ta <*> bevaluatesaandb, and, in case of success, returnsOk (a b). Otherwise, it fails, and the error ofais chosen over the error ofbif both fail.
Infix
module Infix : sig ... endLet 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) tSame 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) tchoose lselects a member oflthat is aOk _value, or returnsError lotherwise, wherelis the list of errors.
val retry : int -> (unit -> ('a, 'err) t) -> ('a, 'err list) tretry n fcallsfat mostntimes, returning the first result off ()that doesn't fail. Ifffailsntimes,retry n ffails with the list of successive errors.
module type MONAD = sig ... end
Conversions
val to_opt : ('a, _) t -> 'a optionConvert a result to an option.
val of_opt : 'a option -> ('a, string) tConvert an option to a result.
val to_std_seq : ('a, _) t -> 'a Seq.t- since
- 2.8