函数返回模块由仿函数创建

时间:2014-11-18 21:55:25

标签: 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 : 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添加到Make_ShipGenerator

修改2 :添加了模块类型SHIPGENERATOR,其中包含ANIMALTYPE所需的部分。

1 个答案:

答案 0 :(得分:0)

嗯,它是ANIMALTYPE,还有什么?

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