let compareOn f x (yobj: obj) =
match yobj with
| :? 'T as y -> compare (f x) (f y)
| _ -> invalidArg "yobj" "cannot compare values of different types"
I don't see how 'T above relates to the type of x. Why is not the type of x just 'a ?
Used in:
type stamp = int
[<CustomEquality; CustomComparison>]
type MyUnionType =
| MyUnionType of stamp * (int -> int)
static member Stamp (MyUnionType (s,_)) = s
override x.Equals y = equalsOn MyUnionType.Stamp x y
override x.GetHashCode() = hashOn MyUnionType.Stamp x
interface System.IComparable with
member x.CompareTo y = compareOn MyUnionType.Stamp x y
The reason why has to do with the uses of
x
. The valuex
andy
are used as arguments to the same call back:f x
andf y
. The type ofy
in this expression is known to beT
hencex
must also be of a type compatible withT
so F# choosesT