Ocaml 由函子创建的函数返回模块

Ocaml 由函子创建的函数返回模块,ocaml,functor,Ocaml,Functor,我在定义函子返回的模块类型时遇到问题。谁能解决这个问题 module type ANIMALTYPE = sig val age : unit -> int end module type SHIPGENERATOR = sig val age : unit -> int (* Another possibility: include ANIMALTYPE *) val hello : string end module RabbitModule : ANIMAL

我在定义函子返回的模块类型时遇到问题。谁能解决这个问题

module type ANIMALTYPE = sig
  val age : unit -> int
end

module type SHIPGENERATOR = sig
  val age : unit -> int
  (* Another possibility: include ANIMALTYPE *)
  val hello : string
end

module RabbitModule : ANIMALTYPE = struct
  let age () = 10
end

module Make_ShipGenerator (A : ANIMALTYPE) = struct
  let age = A.age
  let hello = "world"
end

let get_shipgenerator race = match race with
  | Rabbit -> let module M = (Make_ShipGenerator (RabbitModule)) 
    in (module M : SHIPGENERATOR)
编辑:添加hello以生成ShipGenerator

编辑2:添加模块类型SHIPGENERATOR,其中将包括ANIMALTYPE所需的部分。

好吧,它是ANIMALTYPE,还有什么

let module M = (Make_ShipGenerator (RabbitModule)) 
    in (module M : ANIMALTYPE);;

即使Make_ShipGenerator包含的字段与?中存在的字段不同,您也可以有更多的字段,但不能有更少的字段。它会进行类型检查,但字段hello不可见。请参阅上面的我的更新。是的,因为您使用ANIMALTYPE签名隐藏了它。你想干什么?不确定。但我想我会制作另一个类型的SHIPGENERATOR,函子会返回它,并包含ANIMALTYPE需要的部分。谢谢您可以随时使用解释器检查类型。您可以使用模块签名或模块类型名称module Mk a:T:S=struct强制执行函子的返回类型。。。或者模块Mk:functor A:T->S=functor A:T->struct。