我假设你要比较,并只服用Id
,Name
和SavePath
考虑(按顺序),使得记录行为就好像Tags
等同Category
S为不存在:
open System
open System.Collections.Generic
[<CustomComparison; CustomEquality>]
type Category =
{ mutable Id : string;
Name : string;
SavePath : string;
Tags : HashSet<Tag> }
interface IComparable<Category> with
member this.CompareTo { Id = id; Name = name; SavePath = savePath } =
compare (this.Id, this.Name, this.SavePath) (id, name, savePath)
interface IComparable with
member this.CompareTo obj =
match obj with
| null -> 1
| :? Category as other -> (this :> IComparable<_>).CompareTo other
| _ -> invalidArg "obj" "not a Category"
interface IEquatable<Category> with
member this.Equals { Id = id; Name = name; SavePath = savePath } =
this.Id = id && this.Name = name && this.SavePath = savePath
override this.Equals obj =
match obj with
| :? Category as other -> (this :> IEquatable<_>).Equals other
| _ -> false
override this.GetHashCode() =
hash (this.Id, this.Name, this.SavePath)
and Tag = { Tag : string; }
但是,相反,如果你想通过Name
进行比较,并通过Id
等同然后考虑以下几点:
open System
open System.Collections.Generic
[<CustomComparison; CustomEquality>]
type Category =
{ mutable Id : string;
Name : string;
SavePath : string;
Tags : HashSet<Tag> }
interface IComparable<Category> with
member this.CompareTo { Name = name } =
this.Name.CompareTo name
interface IComparable with
member this.CompareTo obj =
match obj with
| null -> 1
| :? Category as other -> (this :> IComparable<_>).CompareTo other
| _ -> invalidArg "obj" "not a Category"
interface IEquatable<Category> with
member this.Equals { Id = id } =
this.Id = id
override this.Equals obj =
match obj with
| :? Category as other -> (this :> IEquatable<_>).Equals other
| _ -> false
override this.GetHashCode() =
this.Id.GetHashCode()
and Tag = { Tag : string; }