diff --git a/src/core/CCError.ml b/src/core/CCError.ml index 3693361c..1d56ba6b 100644 --- a/src/core/CCError.ml +++ b/src/core/CCError.ml @@ -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 diff --git a/src/core/CCError.mli b/src/core/CCError.mli index 63c966c0..dac93dc1 100644 --- a/src/core/CCError.mli +++ b/src/core/CCError.mli @@ -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