Skip to content

Instantly share code, notes, and snippets.

@leque
Created March 24, 2020 12:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leque/39a66a2badb0999aee9e12e9303d6237 to your computer and use it in GitHub Desktop.
Save leque/39a66a2badb0999aee9e12e9303d6237 to your computer and use it in GitHub Desktop.
module type Show = sig
type t
val show : t -> string
end
module ShowInt : Show with type t = int = struct
type t = int
let show = Int.to_string
end
module ShowFloat : Show with type t = float = struct
type t = float
let show = Float.to_string
end
(* with a functor *)
module F(M : Show) = struct
let f = M.show
end
let x =
let module F_int = F(ShowInt) in
let module F_float = F(ShowFloat) in
F_int.f 42, F_float.f 3.14
(* with a first-class module *)
let f (type a) (module M : Show with type t = a) x = M.show x
let x =
f (module ShowInt) 42, f (module ShowFloat) 3.14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment