Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does not F# provide a custom overload for operator==?

Discriminated unions and other primitive types in F# uses structural equality by default, and provides a generated override for the .Equals method. The F# equality operator apparently differs from the C# one in that it uses the .Equals method even for reference types, but when F# discriminated unions are used from C#, the default operator== for object is used, which checks for reference equality rather than structural equality.

Why does not F# generate a custom operator== for discriminated union types so that == gives the expected behaviour when used in other .NET languages?

like image 739
SoftMemes Avatar asked Sep 23 '10 11:09

SoftMemes


2 Answers

Such behaviour is defined by the language you are using and not by the language of origin of the type you are using.

like image 178
J D Avatar answered Nov 15 '22 07:11

J D


I'm not on the F# team, so I can only speculate, but here are a few potential reasons:

  1. If you want to use structural equality from within C#, you can just use the Equals method. C# provides ways to test for two distinct kinds of equality - why should F# force them to behave in the same way when a might prefer to be able to use reference equality?
  2. If you want to force C# to use structural equality, it's easy to do it yourself:

    type T = A | B of int with
      static member op_Equality(t:T,t2:T) = t = t2
      // or even static member (=)(t:T, t2:T) = t = t2
    
  3. There's a development cost to any feature, so even if there were a clear benefit to automatically generating an op_Equality, it might have been dropped in favor of higher priority features.

like image 20
kvb Avatar answered Nov 15 '22 07:11

kvb