Skip to content

Instantly share code, notes, and snippets.

@Et7f3
Created January 27, 2022 12:28
Show Gist options
  • Save Et7f3/3b4f09ddf5ac1eacf0f4a360d8154d32 to your computer and use it in GitHub Desktop.
Save Et7f3/3b4f09ddf5ac1eacf0f4a360d8154d32 to your computer and use it in GitHub Desktop.
(* just some type to work in one file *)
module Lwt = struct
type 'a t = 'a ref
end
type process_in = int
type ('handler, 'return) onTimeout =
| Catch: float -> (exn -> process_in -> 'return Lwt.t, 'return) onTimeout
| Fail: float -> (process_in -> 'return Lwt.t, 'return) onTimeout
| NoTimeout: (process_in -> 'return Lwt.t, 'return) onTimeout (* for the default value *)
let with_process_in (type handler return) ?(timeout: (handler, return) onTimeout option) (f: handler): return Lwt.t =
let process_in = 13 in
match timeout with
Some (Catch t) ->
Printf.printf "Sleep %f\n" t;
f (Invalid_argument "timeout") process_in
| None -> assert false (* to be typed *)
| Some (Fail t) ->
Printf.printf "Sleep %f\n" t;
if true then
f process_in
else
raise (Invalid_argument "timeout")
| Some NoTimeout -> f process_in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment