Commits

camlspotter committed 8c6fd54

added with_final

  • Participants
  • Parent commits 008acc3

Comments (0)

Files changed (2)

 let try_ignore ~f v = try f v with _ -> ();;
 let try_bool ~f v = try ignore (f v); true with _ -> false
 
+let with_final v f final =
+  match try `Ok (f v) with e -> `Error e with
+  | `Ok res -> final v; res
+  | `Error e -> try_ignore ~f:final v; raise e
+
 let time f v =
   let start = Unix.gettimeofday () in
   let res = f v in

File lib/base.mli

 val try_bool   : f:('a -> 'b) -> 'a -> bool
 (** [true] at success *)
 
+val with_final : 'a -> ('a -> 'b) -> ('a -> unit) -> 'b
+
 (** {6 Imperative operations } *)
 
 val imp : 'a -> ('a ref -> 'b) -> ('a * 'b)