Given a simple factory:
module type Factory = sig type t val create : unit -> t end
module FactoryImpl : Factory = struct
type t = string
let create: unit -> t = fun () -> "aaa"
end
let factory: (module Factory) = (module FactoryImpl)
let f = let module F = (val factory) in F.create ()
Compiler complains:
This has type:
F.t
But somewhere wanted:
F.t
The type constructor F.t would escape its scope
I am quite new to OCaml modules and not sure how to tell compiler that f is of type Factory.t
The problem here is that
F.create ()produces a value of typeF.t, sofshould have typeF.t, but that is impossible becauseFis not bound outside thelet modulethat bindsF.If you extend the scope of
Fto be global, the program will type check:Note that
Factory.tis not a valid type, as there is no module bound to the nameFactory. Modules and module types are in separate namespaces.