好的,有人可以向我解释为什么 F# 允许您重载 > 和 ^ 运算符,但不允许您使用它们吗?
+ (op_Addition): Works just fine.
^ (op_Concatenate): Compiler error in F#. Apparently only strings can be concatenated.
> (op_GreaterThan): Runtime Error – Failure during generic comparison: the type Program+OppTest4 does not implement the System.IComparable interface.
如果我将我的 F# 代码编译为库并使用 VB 中的这些运算符,它们都可以工作。如果我使用 C# 中的这些运算符,除了 op_Concatenate 之外的所有运算符都可以工作(如预期的那样)。但是 F# 不仅忽略了其中的一些,静态类型检查器甚至都懒得告诉你它打算这样做。
编辑代码示例
type OppTest4(value: int) =
member this.value = value
static member (^) (left : OppTest4, right : OppTest4) =
OppTest4( Int32.Parse( left.value.ToString() ^ right.value.ToString() ))
static member (+) (left : OppTest4, right : OppTest4) =
OppTest4(left.value + right.value )
static member (>) (left : OppTest4, right : OppTest4) =
left.value > right.value
static member (<) (left : OppTest4, right : OppTest4) =
left.value < right.value