add CCError.catch, in prevision of the future standard Result.t type

This commit is contained in:
Simon Cruanes 2015-06-09 16:11:54 +02:00
parent ebdf201161
commit fb8e9078a3
2 changed files with 12 additions and 0 deletions

View file

@ -92,6 +92,10 @@ let get_exn = function
| `Ok x -> x
| `Error _ -> raise (Invalid_argument "CCError.get_exn")
let catch e ~ok ~err = match e with
| `Ok x -> ok x
| `Error y -> err y
let flat_map f e = match e with
| `Ok x -> f x
| `Error s -> `Error s

View file

@ -75,6 +75,14 @@ val get_exn : ('a, _) t -> 'a
whenever possible.
@raise Invalid_argument if the value is an error. *)
val catch : ('a, 'err) t -> ok:('a -> 'b) -> err:('err -> 'b) -> 'b
(** [catch e ~ok ~err] calls either [ok] or [err] depending on
the value of [e].
This is useful for code that does not want to depend on the exact
definition of [('a, 'b) t] used, for instance once OCaml gets a
standard [Result.t] type.
@since NEXT_RELEASE *)
val flat_map : ('a -> ('b, 'err) t) -> ('a, 'err) t -> ('b, 'err) t
val (>|=) : ('a, 'err) t -> ('a -> 'b) -> ('b, 'err) t