如何访问 OCaml 递归模块中定义的类型的字段?
How to access a field of a type defined in an OCaml recursive module?
我正在 OCaml 中处理递归模块,但在访问类型字段时遇到了一些问题。
如果我尝试这样做:
module A = struct type t = { name : string; } end
module A2 =
struct
include A
let getName (x:t) = x.name
end;;
一切都很好。但是,我需要一个更复杂的类型,迫使我在递归模块中定义我的类型。
module rec B:Set.OrderedType =
struct
type t = {name: string; set : S.t}
let compare _ _ = 0
end
and S:Set.S = Set.Make (B);;
一切仍然完美。但是,以下模块不正确:
module B2 =
struct
include B
let get_name (x:t) = x.name
end;;
返回的错误是"Unbound record field name"。有什么问题吗?
module rec B:Set.OrderedType =
您的递归定义表明模块 B
具有签名 Set.OrderedType
,它隐藏了 t
的定义,在这种情况下,它的投影。在Set.OrderedType
中,类型t
是抽象的,像这样:type t;;
.
如果要显示类型t
的定义,它必须是签名的一部分。第一个示例有效,因为您没有为模块 A
提供签名,因此默认情况下使用导出所有内容的签名键入它。
下面的示例适用于 OCaml 4.02.1。
module rec B:
sig type t = { name:string ; set : S.t } val compare: t -> t -> int end
=
struct
type t = {name: string; set : S.t}
let compare _ _ = 0
end
and S:Set.S = Set.Make (B);;
顶层这样确认定义:
module rec B :
sig type t = { name : string; set : S.t; } val compare : t -> t -> int end
and S : Set.S
我正在 OCaml 中处理递归模块,但在访问类型字段时遇到了一些问题。
如果我尝试这样做:
module A = struct type t = { name : string; } end
module A2 =
struct
include A
let getName (x:t) = x.name
end;;
一切都很好。但是,我需要一个更复杂的类型,迫使我在递归模块中定义我的类型。
module rec B:Set.OrderedType =
struct
type t = {name: string; set : S.t}
let compare _ _ = 0
end
and S:Set.S = Set.Make (B);;
一切仍然完美。但是,以下模块不正确:
module B2 =
struct
include B
let get_name (x:t) = x.name
end;;
返回的错误是"Unbound record field name"。有什么问题吗?
module rec B:Set.OrderedType =
您的递归定义表明模块 B
具有签名 Set.OrderedType
,它隐藏了 t
的定义,在这种情况下,它的投影。在Set.OrderedType
中,类型t
是抽象的,像这样:type t;;
.
如果要显示类型t
的定义,它必须是签名的一部分。第一个示例有效,因为您没有为模块 A
提供签名,因此默认情况下使用导出所有内容的签名键入它。
下面的示例适用于 OCaml 4.02.1。
module rec B:
sig type t = { name:string ; set : S.t } val compare: t -> t -> int end
=
struct
type t = {name: string; set : S.t}
let compare _ _ = 0
end
and S:Set.S = Set.Make (B);;
顶层这样确认定义:
module rec B :
sig type t = { name : string; set : S.t; } val compare : t -> t -> int end
and S : Set.S