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"
我不知道上面的'T 与x的类型有什么关系。 为什么x的类型不是'a ?
用于:
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
答案 0 :(得分:5)
之所以与x
的使用有关。值x
和y
用作同一回调的参数:f x
和f y
。此表达式中y
的类型已知为T
,因此x
也必须属于与T
兼容的类型,因此F#选择T