比较 OCaml 中的自定义类型

Compare a custom type in OCaml

我创建了自己的类型

type mine = int * int

我创建了一个构造函数

val cons: int -> int -> mine

现在我想编写几个单元测试来检查构造函数是否创建了我期望的结果

open OUnit2;;
let tst _ = assert_equal (7, 15) (Foo.cons 14 45)

但随后编译比较了类型(预期int*int但得到Foo.mine。但实际上它是相同的类型。

是否可以比较两个这样的值?

type mine = int * int这样的类型定义只是一个缩写。所以原则上没有什么可以阻止比较。

# type mine = int * int;;
type mine = int * int
# let cons a b : mine = (a, b);;
val cons : int -> int -> mine = <fun>
# compare (3, 3) (cons 3 3);;
- : int = 0

所以,本质就在你的模块的接口里Foo