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
, sof
should have typeF.t
, but that is impossible becauseF
is not bound outside thelet module
that bindsF
.If you extend the scope of
F
to be global, the program will type check:Note that
Factory.t
is not a valid type, as there is no module bound to the nameFactory
. Modules and module types are in separate namespaces.