我定义了两种记录类型:
type name =
{ r0: int; r1: int; c0: int; c1: int;
typ: dtype;
uid: uid (* key *) }
and func =
{ name: string;
typ: dtype;
params: var list;
body: block }
后来我得到了一行代码的错误:
Error: The record field label typ belongs to the type Syntax.func but is mixed here with labels of type Syntax.name
谁能告诉我我们是否不应该让两个记录的两个字段具有相同的标签,例如
typ
在这里,这使编译器感到困惑。
最佳答案
不,你不能,因为它会破坏类型推断。
顺便说一句,您可以使用模块命名空间来解决这个问题:
module Name = struct
type t = { r0:int; ... }
end
module Func = struct
type t = { name: string; ... }
end
稍后,您可以在字段名称前加上正确的模块:
let get_type r = r.Name.typ
let name = { Name.r0=1; r1=2; ... }
let f = { Func.name="foo"; typ=...; ... }
请注意,您只需要为第一个字段添加前缀,编译器会自动理解您正在编写的值的类型。
关于types - 两条记录的两个字段在 OCaml 中具有相同的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928970/