我有一个问题,我从三天起就不明白,因为我不明白,我无法解决.
我有这样的代码:
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
Run Code Online (Sandbox Code Playgroud)
问题来自(=)我已经超载.
一旦我拥有它,编译器坚持有以下反应:
Error: This expression has type string but an expression was expected of type t
Run Code Online (Sandbox Code Playgroud)
我试图声明(=)的签名但它不起作用.
例如,这给出了同样的事情:
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
Run Code Online (Sandbox Code Playgroud)
如果我在isIn之后移动(=),它可以工作,但是一旦我开始添加更多逻辑,它就会产生相同的错误.所以我不知道会发生什么.
谁有人向我解释这个?谢谢!
OCaml中没有函数重载.一旦定义了具有给定名称的函数(或该问题的任何其他类型的值),该名称将覆盖具有相同名称的任何现有值,只要它在范围内.
因此,一旦定义了全局=函数=,除了通过其完全限定名称之外,不再可以访问旧文件的其余部分Pervasives.=.