F# and Operator Overloads: (>) and (^)

后端 未结 2 1824
北恋
北恋 2021-01-14 07:45

Ok, so can someone explain to me why F# allows you to overload the > and ^ operators, but doesn\'t allow you to use them?

+ (op_Addition): Works just fine.
^         


        
2条回答
  •  半阙折子戏
    2021-01-14 08:30

    I agree, there is inconsistency: operator can be defined, but can't be used.

    Are you asking, why F# designers decided to implement comparison with System.IComparable interface rather than operators overload? I don't know why, but in OO language I would prefer IComparable rather than operators overloading. So, I would suggest to F# developers to break C# compatibility and forbid "static member (>) (...)" syntax sugar.

    If you are asking how to call these overloaded operators it is pretty easy: use op_Concatenate, op_GreaterThan or op_LessThan static members. (Really, I've got a compiler warning, describing the problem. F# 1.9.6.16)

    Runtime Error casting to System.IComparable without any compiler warning definitely is a bug. You can send it to fsbugs@microsoft.com.

提交回复
热议问题