为什么无法在OCaml中强制转换记录类型?像
下面是一个示例,我构建了一个名为
int
这样的基本类型可以正常工作。下面是一个示例,我构建了一个名为
M
的基本模块,并将其包含在模块 A
中。在 A
模块中,M.t
被缩写为类型。只要 M.t
是 int
,我就可以执行 A.t' :> M.t
。当我将其更改为 {i : int}
时,编译器会报告它不是子类型。我猜这里有一个非常具体的原因?module M = struct
type t = {i : int}
let make () = {i = 10}
end
module A : sig
include module type of M
type t' = private t
val make : unit -> t'
end = struct
include M
type t' = t
end
在顶层:
(A.make() :> M.t);;
Error: Type A.t' is not a subtype of M.t
include module type of M with type t = M.t
。翻译完毕。 - hcartyint
这样的基本类型却不需要吗?如果你把 t 改成type t = int
,上述代码就会起作用。 - Olle Härstedttype t = int
和type t' = int
中,t
和t'
是现有类型的别名(类型缩写),可以互换使用。但是,在type t = {x : int}
和type t' = {x:int}
中,这些是两种新类型,没有任何关系。 - ygrek