Jac*_*ack 14 ocaml module functor
我有以下情况:
module type M = sig type s = ... end
module Make(P: Something) : (M with type s = P.t) = struct
type s = P.t
...
end
Run Code Online (Sandbox Code Playgroud)
这适用于生成在其实现M中使用类型模块的特定实现的类型的模块Something.
现在假设我有另一个模块定义为
module type AU = sig
val feed : float -> unitv
val nth : int -> (float -> float)
val reset : unit -> unit
end
Run Code Online (Sandbox Code Playgroud)
有各种实现
module SUAlg : AU = struct ... end
module MLAlg : AU = struct ... end
module ACEAlg : AU = struct ... end
Run Code Online (Sandbox Code Playgroud)
问题的关键在于M模块现在应该通过两个方面进行参数化:一个Something模块和一个AU模块,这样它就像
module Make(P: Something) : (M with type s = P.t) = struct
type s = P.t
module Alg = MLAlg (* just an example *)
...
end
Run Code Online (Sandbox Code Playgroud)
但是我希望有一个通用仿函数,它给出了一个Something并且给出了AU它产生一个具有两个具体化的东西的模块.有没有办法轻松获得?
仿函数语法很奇怪,我还是新手,我不知道我问的问题是否可以用简单的方法解决.
提前致谢
Pas*_*uoq 19
是的,仿函数可以有几个参数.语法是这样的:
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
Run Code Online (Sandbox Code Playgroud)
然后可以应用仿函数Make_LOffset(V)(LOffset).
在这个例子中,取自现有代码以确保它在语法上是正确的,Make_LOffset由两个模块V和LOffset相应的签名Lattice_With_Isotropy.S和参数化Offsetmap.S.两个签名之间还有其他类型约束,即with type … and type …部分.