Skip to content

Commit

Permalink
Raise Failure in Kdl.L.{get,set}_exn
Browse files Browse the repository at this point in the history
Use failwith instead of Option.get
  • Loading branch information
eilvelia committed Oct 15, 2023
1 parent 941ce78 commit 706da8e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/kdl.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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]. *)
Expand Down
12 changes: 10 additions & 2 deletions src/lens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)
Expand Down

0 comments on commit 706da8e

Please sign in to comment.