Reputation: 1102
I have this functor that has a submodule:
module type PT = sig
type t
val to_string: t -> string
end
module A(P:PT) = struct
module SubA = struct
type t = T of P.t | F of float
end
end
And I want to extend it using an include. This even works:
module Aplus(P:PT) = struct
include A(P)
let print x = print_string (P.to_string x)
end
But for some reason it doesn't work for submodules.
module Aplus(P:PT) = struct
include A(P)
let print x = print_string (P.to_string x)
module SubAplus = struct
include A(P).SubA (* Syntax error here *)
end
end
It fails with a Syntax error
on the submodule reference after a functor application. That is quite strange, since it looks like the language grammar allows that. Is there any specific reason for disallowing that?
Upvotes: 2
Views: 135
Reputation: 3739
Easy enough:
module M = F(A)
include M.SubModule
There are various possible reason, but one simple is: if the module resulting of the functor application F(A)
is not named, you will not be able to refer to it. If the function introduces type equalities in the context, it will lead to issues (and in error messages in particular ...)
Note that it's possible to do it with types, module types and class types. F(M).M.t
is a perfectly valid type expression.
To finish, in your case, you don't need to reapply the functor, you already did earlier, so include SubA
will be enough.
Upvotes: 2
Reputation: 35210
OCaml syntax in a module path, allows you only to reference to module types, type constructors and types:
For referring to type constructors, module types, or class types, the prefix can also contain simple functor applications (as in the syntactic class extended-module-path above) in case the defining module is the result of a functor application.
In other words to access to real values, you need to instantiate your module, and then you can reference any values. See Drup's answer for a complete example.
Upvotes: 2