为什么一旦 (=) 被重载,编译器总是想要这个类型?
Why the compiler always wants this type once (=) is overloaded?
我有一个问题三天没看懂,既然看不懂,就解决不了。
我有这样的代码:
module SpotLocation = struct
type t = {
uuid : string option;
netElement : string;
coordIntrinsic : float;
}
end
module Segment = struct
type t ={
netElement : string;
coordFrom : SpotLocation.t;
coordTo : SpotLocation.t;
}
let isEqual segA segB = segA.coordFrom = segB.coordFrom && segA.coordTo = segB.coordTo
let (=) = isEqual (* <<<<<<<<< Here is the problem *)
let isIn seg loc = (seg.netElement = loc.netElement)
end
问题出在(=)我超载了。
一旦我有了它,编译器坚持有以下反应:
Error: This expression has type string but an expression was expected of type t
我已尝试声明 (=) 的签名,但它不起作用。
例如,这给出了同样的东西:
module Segment = struct
type t ={
netElement : string;
coordFrom : SpotLocation.t;
coordTo : SpotLocation.t;
}
let isEqual segA segB = segA.coordFrom = segB.coordFrom && segA.coordTo = segB.coordTo
let ((=) : t -> t -> bool) = isEqual (* <<<<<<<<< Here is the problem *)
let isIn (seg : t) (loc : SpotLocation.t) =
let open SpotLocation in
seg.netElement = loc.netElement
end
如果我将 (=) 移到 isIn 之后,它会起作用,但是一旦我开始添加更多逻辑,它就会出现同样的错误。所以我不知道会发生什么。
有人能给我解释一下吗?谢谢!
OCaml 中没有函数重载。使用给定名称定义函数(或与此相关的任何其他类型的值)后,只要该名称在范围内,该名称就会隐藏任何具有相同名称的现有值。
因此,一旦您定义了全局 =
函数,旧 =
文件的其余部分将不再可访问,除非通过其完全限定名称 Pervasives.=
.
我有一个问题三天没看懂,既然看不懂,就解决不了。
我有这样的代码:
module SpotLocation = struct
type t = {
uuid : string option;
netElement : string;
coordIntrinsic : float;
}
end
module Segment = struct
type t ={
netElement : string;
coordFrom : SpotLocation.t;
coordTo : SpotLocation.t;
}
let isEqual segA segB = segA.coordFrom = segB.coordFrom && segA.coordTo = segB.coordTo
let (=) = isEqual (* <<<<<<<<< Here is the problem *)
let isIn seg loc = (seg.netElement = loc.netElement)
end
问题出在(=)我超载了。
一旦我有了它,编译器坚持有以下反应:
Error: This expression has type string but an expression was expected of type t
我已尝试声明 (=) 的签名,但它不起作用。
例如,这给出了同样的东西:
module Segment = struct
type t ={
netElement : string;
coordFrom : SpotLocation.t;
coordTo : SpotLocation.t;
}
let isEqual segA segB = segA.coordFrom = segB.coordFrom && segA.coordTo = segB.coordTo
let ((=) : t -> t -> bool) = isEqual (* <<<<<<<<< Here is the problem *)
let isIn (seg : t) (loc : SpotLocation.t) =
let open SpotLocation in
seg.netElement = loc.netElement
end
如果我将 (=) 移到 isIn 之后,它会起作用,但是一旦我开始添加更多逻辑,它就会出现同样的错误。所以我不知道会发生什么。
有人能给我解释一下吗?谢谢!
OCaml 中没有函数重载。使用给定名称定义函数(或与此相关的任何其他类型的值)后,只要该名称在范围内,该名称就会隐藏任何具有相同名称的现有值。
因此,一旦您定义了全局 =
函数,旧 =
文件的其余部分将不再可访问,除非通过其完全限定名称 Pervasives.=
.