Functors with multiple arguments in OCaml
I've the following situation:
module type M = sig type s = ... end
module Make(P: Something) : (M with type s = P.t) = struct
type s = P.t
...
end
that works fine to generate modules of M
type that use specific implementation of modules of type Something
inside their implementation.
Now suppose I have another module defined as
module type AU = sig
val feed : float -> unitv
val nth : int -> (float -> float)
val reset : unit -> unit
end
that has various implementations
module SUAlg : AU = struct ... end
module MLAlg : AU = struct ... end
module ACEAlg : AU = struct ... end
The point of the question is that the M
module should be parametrized over two things now: a Something
module and a AU
module so that it's something like
module Make(P: Something) : (M with type s = P.t) = struct
type s = P.t
module Alg = MLAlg (* just an example *)
...
end
but I would like to have a generic functor that given a Something
and given an AU
it produces a module with both things concretized. Is there a way to obtain that easily?
Since functor syntax is quite strange and I'm still new to it I d开发者_Go百科on't know if what I'm asking can be solved in a simple way or not.
Thanks in advance
Yes, a functor can have several arguments. The syntax is like this:
module Make_LOffset
(V:Lattice_With_Isotropy.S)
(LOffset : Offsetmap.S with type y = V.t and type widen_hint = V.widen_hint) =
struct
…
end
The functor can then be applied with Make_LOffset(V)(LOffset)
.
In this example, taken from existing code to ensure it is syntactically correct, Make_LOffset
is parameterized by two modules V
and LOffset
, of respective signatures Lattice_With_Isotropy.S
and Offsetmap.S
. There are additional type constraints between the two signatures, the with type … and type …
part.
精彩评论