如果我尝试编写一个调用 =
的参数化模块在参数提供的未指定类型上,SML/NJ 会引发类型错误。例如,如果我有签名
signature SIG =
sig
type t
end
并尝试参数化一个模块
F
在一个模块上S
带有签名SIG
functor F (S : SIG) =
struct
fun f (x:S.t) (y:S.t) = (x = y)
end
我将触发以下编译错误:
Error: operator and operand don't agree [equality type required]
operator domain: ''Z * ''Z
operand: S.t * S.t
in expression:
x = y
如何指定
S.t
应该是平等类型?到目前为止,我能够弄清楚的唯一解决方法是在仿函数参数化的结构中提供相等函数,即:
signature SIG' =
sig
type t
val eq : (''a * ''a) -> bool
end
functor F' (S' : SIG') =
struct
fun f' x y = S'.eq (x, y)
end
structure A = F'( struct
type t = int
val eq = (op =)
end )
似乎必须有更好的方法,尽管我也可能误解了仿函数是如何工作的一些基本和重要的东西。
最佳答案
您只需指定 t
是 eqtype
:
signature SIG =
sig
eqtype t
end
关于types - 有没有办法约束仿函数的参数签名,以便参数可以为结构提供未指定的相等类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33970353/