module type ELEMENT =
sig
type element_i
end
module Element:ELEMENT =
struct
type element_i = N of int | CNN of cnn
end
module type SEMIRING =
functor (E:ELEMENT)->
sig
type elements
end
module Semiring:SEMIRING =
functor(Element:ELEMENT) ->
struct
let zero = (Element.element_i 0) (*ERROR: Unbounded Value; Same with N 0*)
end
如何在 Semiring 模块中创建类型为 element_i 的对象?
最佳答案
您可以允许程序员在 Semiring
中创建类型为 element_i
的值,方法是不隐藏这种类型的构造函数,就像您目前正在做的那样。
相反,将签名 ELEMENT
定义为:
module type ELEMENT =
sig
type element_i = N of int | CNN of cnn
end
这让你的仿函数 Semiring
期望它的 Element
参数更多:而不是任何类型 Element.element_i
,它现在只接受一个类型正是这些构造函数。但从好的方面来说,它现在可以应用构造函数来构建这种类型的值,例如 Element.N 12
关于module - 跨模块的类型定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4076177/