如何在 OCaml 中“公开”类型

时间:2021-04-27 14:41:25

标签: ocaml

我有两个编译单元和一个“前端”文件。

在第一个编译单元中,我在 moduleA.mli 中有以下内容:

module A : sig
   type t
   ... 
end

moduleA.ml 中我有:

module A = struct
   type t = float * float
   ... 
end

在第二个编译单元中,moduleB.mli 中有以下内容:

module B : sig
   type t
   ... 
end

moduleB.ml 中我有:

open moduleA

module B = struct
   type t = A.t
   ... 
end

现在,在“前端”文件中,我收到如下类型错误:

This expression has type moduleB.B.t but an expression was expected of type moduleA.A.t

然而,正如我所定义的,这些应该是同义词(两者都是 float * float)。我怎样才能“公开”这些类型,以便 Ocaml 知道它们是相同的?

1 个答案:

答案 0 :(得分:2)

当你把一个模块拆分成一个签名和一个实现时,只有签名中的信息可以被其他模块使用,其他任何东西都是实现私有的。因此,当其他模块查看您的模块 AB 时,他们看到的只是 type t 和另一个 type t,并且他们不知道它们指的是同一类型。< /p>

如果您想公开 A.t = B.t 的信息,该信息需要成为签名的一部分。因此,只需将 type t 的签名中的 B 更改为 type t = A.t,现在这种相等性将成为模块公共接口的一部分,并且可以被其他模块依赖。

相关问题