Xem Don's blog đăng bài về chủ đề này, cụ thể là phần Tuỳ chỉnh bình đẳng và so sánh.
Ví dụ ông cho là gần giống như cấu trúc lại được đề xuất:
/// A type abbreviation indicating we’re using integers for unique stamps on objects
type stamp = int
/// A type containing a function that can’t be compared for equality
[<CustomEquality; CustomComparison>]
type MyThing =
{ Stamp: stamp;
Behaviour: (int -> int) }
override x.Equals(yobj) =
match yobj with
| :? MyThing as y -> (x.Stamp = y.Stamp)
| _ -> false
override x.GetHashCode() = hash x.Stamp
interface System.IComparable with
member x.CompareTo yobj =
match yobj with
| :? MyThing as y -> compare x.Stamp y.Stamp
| _ -> invalidArg "yobj" "cannot compare values of different types"
Nguồn
2012-06-09 03:03:58
+1 quảng cáo :-) –