types - 有没有办法约束仿函数的参数签名,以便参数可以为结构提供未指定的相等类型?

标签 types module sml equality smlnj

如果我尝试编写一个调用 = 的参数化模块在参数提供的未指定类型上,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 )

似乎必须有更好的方法,尽管我也可能误解了仿函数是如何工作的一些基本和重要的东西。

最佳答案

您只需指定 teqtype :

signature SIG =
sig
  eqtype t
end

关于types - 有没有办法约束仿函数的参数签名,以便参数可以为结构提供未指定的相等类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33970353/

相关文章:

python - 返回属性名称和类型值的字典

perl - 将 Perl 模块作为插件实现到最小核心

ios - Xcode 6.3.1 错误 : Timed out waiting to acquire lock file for module 'X' where 'X' is my framework

functional-programming - 标准 ML 重复最后一个命令,向左箭头?

sml - 如何从源代码安装 sml/nj?

c - char 数据类型在 C 中以 32 位和 64 位存储或表示的方式有何区别?

scala - "String with Int"是什么意思?

c# - 比较通用接口(interface)类型

angular - ng2-translate 在延迟加载模块中不起作用

sml - 标准 ML 中的结构比较