Ocaml 为什么编译器在(=)重载后总是需要此类型?

Ocaml 为什么编译器在(=)重载后总是需要此类型?,ocaml,Ocaml,我有一个三天以来一直不明白的问题,因为我不明白,我无法解决它 我有这样的代码: module SpotLocation = struct type t = { uuid : string option; netElement : string; coordIntrinsic : float; } end module Segment = struct type t ={ netElement : string; coordFro

我有一个三天以来一直不明白的问题,因为我不明白,我无法解决它

我有这样的代码:

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
我试图声明(=)的签名,但无效

例如,这给出了相同的结果:

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
模块段=结构
t型={
netElement:string;
coordFrom:SpotLocation.t;
coordTo:SpotLocation.t;
}
设isEqual segA segB=segA.coordFrom=segB.coordFrom&&segA.coordTo=segB.coordTo

let((=):t->t->bool)=isEqual(*OCaml中没有函数重载。一旦定义了一个具有给定名称的函数(或任何其他类型的值),只要该名称在范围内,该名称就会对具有相同名称的任何现有值进行阴影处理


因此,一旦您定义了全局
=
函数,旧的
=
对于文件的其余部分将不再可访问,除非通过其完全限定名
普及版。=

那么我可以仅为我的类型定义(=)吗?@Enzojz您只为您的类型定义了
(=)
。您不能做的是让您的
(=)
和默认的
(=)
同时在作用域中(并根据类型决定调用哪个)。正如我所说:OCaml没有函数重载。好的,明白了。谢谢!@Enzojz如果我没有弄错的话,在FP中操作符重载是非常重要的,因为可能很难判断在运行时调用哪个函数,这会使程序的推理复杂化。此外,OCaml希望从操作符推断操作数类型阿托尔。
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