From e8617c4f05f302c79c7c7d184f355749a39f7ba8 Mon Sep 17 00:00:00 2001 From: Hongchang Wu Date: Thu, 4 Oct 2018 20:53:03 -0400 Subject: [PATCH] Add CCResult.get_or_failwith --- src/core/CCResult.ml | 9 +++++++++ src/core/CCResult.mli | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/src/core/CCResult.ml b/src/core/CCResult.ml index 362d7b4d..12205866 100644 --- a/src/core/CCResult.ml +++ b/src/core/CCResult.ml @@ -93,6 +93,15 @@ let get_or e ~default = match e with | Ok x -> x | Error _ -> default +let get_or_failwith = function + | Ok x -> x + | Error msg -> failwith msg + +(*$T + get_or_failwith (Ok 1) = 1 + try ignore @@ get_or_failwith (Error "e"); false with Failure msg -> msg = "e" +*) + let map_or f e ~default = match e with | Ok x -> f x | Error _ -> default diff --git a/src/core/CCResult.mli b/src/core/CCResult.mli index e12ea948..c50c6b81 100644 --- a/src/core/CCResult.mli +++ b/src/core/CCResult.mli @@ -84,6 +84,11 @@ val get_exn : ('a, _) t -> 'a val get_or : ('a, _) t -> default:'a -> 'a (** [get_or e ~default] returns [x] if [e = Ok x], [default] otherwise. *) +val get_or_failwith : ('a, string) t -> 'a +(** [get_or_failwith e] returns [x] if [e = Ok x], fails otherwise. + @raise Failure with [msg] if [e = Error msg]. + @since NEXT_RELEASE *) + val map_or : ('a -> 'b) -> ('a, 'c) t -> default:'b -> 'b (** [map_or f e ~default] returns [f x] if [e = Ok x], [default] otherwise. *)