Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom comparison and equality in fsharp

Tags:

comparison

f#

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

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

like image 643
nicolas Avatar asked Mar 02 '12 16:03

nicolas


1 Answers

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).

like image 69
JaredPar Avatar answered Sep 30 '22 05:09

JaredPar