diff --git a/core/CCOpt.ml b/core/CCOpt.ml index cc087ca2..40bd8e58 100644 --- a/core/CCOpt.ml +++ b/core/CCOpt.ml @@ -92,6 +92,10 @@ let fold f acc o = match o with | None -> acc | Some x -> f acc x +let get default x = match x with + | None -> default + | Some y -> y + let get_exn = function | Some x -> x | None -> invalid_arg "CCOpt.get_exn" diff --git a/core/CCOpt.mli b/core/CCOpt.mli index cd783f77..48b7ec4a 100644 --- a/core/CCOpt.mli +++ b/core/CCOpt.mli @@ -60,6 +60,10 @@ val iter : ('a -> unit) -> 'a t -> unit val fold : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a (** Fold on 0 or 1 elements *) +val get : 'a -> 'a t -> 'a +(** [get default x] unwraps [x], but if [x = None] it returns [default] instead. + @since NEXT_RELEASE *) + val get_exn : 'a t -> 'a (** Open the option, possibly failing if it is [None] @raise Invalid_argument if the option is [None] *)