From 706da8e4260f3f951e7a3b9985b0e50bd880601f Mon Sep 17 00:00:00 2001 From: Bannerets Date: Sun, 15 Oct 2023 17:56:28 +0000 Subject: [PATCH] Raise Failure in Kdl.L.{get,set}_exn Use failwith instead of Option.get --- src/kdl.mli | 6 ++++-- src/lens.ml | 12 ++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/kdl.mli b/src/kdl.mli index 83fc92c..7240daf 100644 --- a/src/kdl.mli +++ b/src/kdl.mli @@ -221,10 +221,12 @@ module L : sig (** Run the setter ([lens.set]). *) val get_exn : 'a -> ('a, 'b) lens -> 'b - (** [get_exn a l] is [Option.get (get a l)]. *) + (** [get_exn a l] is a raising version of [get a l]. + @raise Failure *) val set_exn : 'a -> 'b -> ('a, 'b) lens -> 'a - (** [set_exn a v l] is [Option.get (set a v l)]. *) + (** [set_exn a v l] is a raising version of [set a v l]. + @raise Failure *) val (.@()) : 'a -> ('a, 'b) lens -> 'b option (** [(.@())] is an indexing operator for [get]. *) diff --git a/src/lens.ml b/src/lens.ml index adc1e28..6ae16f5 100644 --- a/src/lens.ml +++ b/src/lens.ml @@ -11,9 +11,17 @@ let get a lens = lens.get a let set a v lens = lens.set v a -let get_exn a lens = Option.get @@ lens.get a +let error () = failwith "Lens failed to match" -let set_exn a v lens = Option.get @@ lens.set v a +let get_exn a lens = + match lens.get a with + | Some v -> v + | None -> error () + +let set_exn a v lens = + match lens.set v a with + | Some v -> v + | None -> error () (* update can be added to the definition of [lens] to increase performance with more specialized implementations *)