- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否有可能以某种方式实现 IComparable
对于 HashSet<'a>
?原因是我声明了以下记录:
[<StructuralComparison>]
type Category = {
mutable Id: string;
Name: string;
SavePath: string;
Tags: HashSet<Tag> }
and Tag = { Tag:string; }
Category
中添加标签记录类型为
HashSet<Tag>
- 为了将一系列类别映射到 map ,我需要实现
IComparable
不知何故......否则它只会导致:
The struct, record or union type 'Category' has the 'StructuralComparison' attribute but the component type 'HashSet' does not satisfy the 'comparison'
HashSet<'a>
我不能使用其他任何东西因为我正在使用的数据库完全理解任何 fsharp-ish 列表。
最佳答案
我假设你想比较和等同 Category
s 只服用 Id
, Name
, 和 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; }
关于F# -> 为 HashSet<'a> 实现 IComparable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5557899/
我如何实现 IComparer int CompareTo(object obj) 有一个额外的参数,比如 int CompareTo(object obj, Dictionary preferenc
我在正确使用 IComparable<> 时遇到了一些麻烦界面。我还创建了一个实现 IComparer<> 的类.我几乎从一本书中复制了确切的编码示例,但对其进行了重新设计以适应我的情况,但它似乎无法
我是否应该同时实现 IComparable和通用 IComparable ?如果我只实现其中之一,是否有任何限制? 最佳答案 是的,您应该同时实现两者。 如果你实现其中一个,任何依赖于另一个的代码都会
这个问题在这里已经有了答案: When to use IComparable Vs. IComparer (8 个答案) 关闭 4 年前。 IComparable 和IComparer 接口(int
我正在尝试实现此链接的相同示例,但更注重受抚养 child 的数量。 http://www.codeproject.com/Articles/42839/Sorting-Lists-using-ICo
在大多数地方,我读到从 IComparable 继承是个好主意。和 IComparable在你的类中提供与非通用集合的兼容性。我的问题是为什么IComparable不继承自 IComparable那么
我正在使用 WPF 博士的 ObservableSortedDictionary。 构造函数如下所示: public ObservableSortedDictionary(IComparer comp
我知道 IComparable 之间有很大的区别和 IComparable一般情况下,请参阅 this , 但在这种搜索方法中,它不会有任何区别,还是会这样? public static int Se
我是 C# 的新手(6 个月的工作经验),但它看起来与 Java 非常相似,所以我感觉很自在。 但是,今天我尝试实现 IComparer 接口(interface)并想知道为什么它会给我一个错误: p
我有 Employee 类,我需要实现 IComparable 并使用 CompareTo 方法按姓名对员工进行排序。据我所知,我必须返回 1、-1 和 0,但我该如何使用这些字符串? 这是我的。 c
是否可以使用 iComparer 使用对象中的两个值对列表进行排序? 我有一个基于 value1 排序的自定义比较器类。但是对 value1 和 value2 进行排序的最佳方法是什么? 按 valu
我为装箱的 RegistryItem 对象定义了以下 IComparer: public class BoxedRegistryItemComparer : IComparer { publi
我正在尝试在相似值列表中获取唯一值,仅通过管道分隔字符串中的一个元素来区分...我不断得到至少一个对象必须实现 Icomparable。我不明白为什么我总是得到这个。我能够按该值进行分组...为什么我
假设我们有这 3 个类: Class BaseClass : System.IComparable { [int] $Value BaseClass([int] $v) {
所以我在这个错误上画了一个空白。无法比较数组中的两个元素。Array.Sort(患者);是产生错误的地方。我确实有一个 IComparable 接口(interface)和一个包含以下代码的类文件:T
我刚开始尝试使用 IEnumerable 接口(interface)。我总是只编写自定义哈希排序而不是尝试使用 native 语法,因为我对实现有些困惑。我正在尝试确定是否可以使用 BinarySea
我有一个 MyClass 类,我想像这样把它作为字典的键: Dictionary dict = new Dictionary(); 我想确保 MyClass 是唯一键,唯一性是通过查看 MyClass
在下面的代码中,我以 .NET 2.0 Framework 为目标。 我可以将 Programmer(派生)对象传递给需要 Person(基类)的 Compare 方法 但由于程序员是一个人(简单的
我正在尝试使用 IComparer 对对象数组进行排序。 我编写了代码,但它仅适用于特定对象。例如: 这门课 public class Cars { public string Name {
如何实现需要参数的 IComparer(可能不相关,但我在 Linq 查询中使用它)? 我想它应该这样称呼: ListOfObjectsToSort.orderBy(x => x, myCustomC
我是一名优秀的程序员,十分优秀!