Skip to content

Instantly share code, notes, and snippets.

@komamitsu
Created September 15, 2014 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komamitsu/d78249b9a32c3979e94a to your computer and use it in GitHub Desktop.
Save komamitsu/d78249b9a32c3979e94a to your computer and use it in GitHub Desktop.
Practice of First-class module (generate new module)
# module type Bumpable = sig
type t
val bump : t -> t
end;;
module type Bumpable = sig type t val bump : t -> t end
# module Int_bumper = struct
type t = int
let bump n = n + 1
end;;
module Int_bumper : sig type t = int val bump : t -> t end
# let int_bumper = (module Int_bumper : Bumpable with type t = int);;
val int_bumper : (module Bumpable with type t = int) = <module>
# let create_newbump (type a) (module B : Bumpable with type t = a) =
(module struct
type t = B.t
let bump n = B.bump (B.bump n)
end : Bumpable with type t = a)
;;
val create_newbump : (module Bumpable with type t = 'a) -> (module Bumpable with type t = 'a) = <fun>
# let nb = create_newbump int_bumper;;
val nb : (module Bumpable with type t = int) = <module>
# module NB = (val nb : Bumpable with type t = int);;
module NB : sig type t = int val bump : t -> t end
# NB.bump 3;;
- : int = 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment