是否有可能以某种方式实施IComparable
for a HashSet<'a>
?原因是我声明了以下记录:
[<StructuralComparison>]
type Category = {
mutable Id: string;
Name: string;
SavePath: string;
Tags: HashSet<Tag> }
and Tag = { Tag:string; }
如您所见,然后标签中Category
记录的类型HashSet<Tag>
- 为了将一系列类别映射到地图,我需要实现IComparable
不知怎的......否则它只会导致:
结构体、记录或联合类型
“类别”有
'StructuralComparison' 属性但是
组件类型“HashSet”的作用
不满足“比较”
请注意,除了 a 之外我不能使用任何其他东西HashSet<'a>
因为我正在使用的数据库根本不理解任何尖锐的列表。
我假设你想要比较并等同Category
s 通过仅采取Id
, Name
, and SavePath
考虑到(按顺序),使记录的行为就像Tags
不在场:
open System
open System.Collections.Generic
[<CustomComparison; CustomEquality>]
type Category =
{ mutable Id : string;
Name : string;
SavePath : string;
Tags : HashSet<Tag> }
member private this.Ident = this.Id, this.Name, this.SavePath
interface IComparable<Category> with
member this.CompareTo other =
compare this.Ident other.Ident
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 other =
this.Ident = other.Ident
override this.Equals obj =
match obj with
| :? Category as other -> (this :> IEquatable<_>).Equals other
| _ -> false
override this.GetHashCode () =
hash this.Ident
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; }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)