nicolas
nicolas

Reputation: 9805

Custom comparison and equality in fsharp

It seems one has to override Equality in order to override comparison.

Is it true ? Is there any reason I am missing ?

Upvotes: 7

Views: 3178

Answers (1)

JaredPar
JaredPar

Reputation: 754763

No. It is possible to only have custom comparison

[<CustomComparison>]
[<StructuralEquality>]
type Node =
    | Data of string
    | Nil

    with

    interface System.IComparable with 
        member x.CompareTo y = 0

Note though that this code will produce a warning recomending that you implement equality on the type as well. This is generally a good idea. If you go through the trouble of implementing comparison then equality straight forward (Compare == 0).

Upvotes: 8

Related Questions