Created
March 24, 2020 12:02
-
-
Save leque/39a66a2badb0999aee9e12e9303d6237 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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