我有四个模块。
Ring,Field是Ring的子模块,Coefficient是Ring的子模块,Coefficientdivisible是Ring和Coefficicent的子模块。
module type Ring =
sig
type t
val add : t -> t -> t
multiply : t -> t -> t
val zero : t
val one : t
val opposite : t (* a+ opposite a =0*)
end
module type Field =
sig
include Ring
val division : t -> t-> t
end
module type Coefficient =
sig
include Ring
val stringDeCoef : t -> string
end
module type Coefficientvisible =
sig
include Field
include Coefficient
end
当我尝试编译前三个模块时,它不会产生问题,但第四个模块返回错误消息,ocamlc 说:
File "CoefficientDivisible.ml", line 7, characters 1-20: Error: Multiple definition of the type name t. Names must be unique in a given structure or signature.
你有解决办法吗?
最佳答案
破坏性替代通常是解决多重定义问题的答案:
module type CoefficientDivisible = sig
include Field
include Coefficient with type t := t
end
另一种选择是使用更小的扩展模块类型并将它们组合起来 显式生成基本模块类型的扩展版本。例如, 具有以下扩展模块类型:
module type RingToField = sig
type t
val division: t -> t -> t
end
和
module type RingToCoefficient =
sig
type t
val stringOfCoef : t -> string
end
模块类型Ring
、Field
和CoefficientDivisible
具有直接的定义:
module type Field = sig
include Ring
include RingToField with type t := t
end
module type Coefficient = sig
include Ring
include RingToCoefficient with type t := t
end
module type CoefficientDivisible = sig
include Field
include RingToCoefficient with type t := t
end
关于ocaml - OCaml 模块的菱形继承(钻石问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50507847/