F# 使用自定义类设置
我正在尝试将 Set 操作与我拥有的类一起使用。 该类的每个实例都有一个唯一的 ID。 我是否需要实现 System.IComparable 接口?如果需要,我该如何实现?
type SomeClass(id : int) =
member this.ID = id
let someSet = Set.of_list [SomeClass(1); SomeClass(2)]
let test = someSet.Contains(SomeClass(2))
I'm trying to use Set operations with a class that I have. Every instance of this class has a unique ID. Do I need to implement the System.IComparable interface and if so how would I?
type SomeClass(id : int) =
member this.ID = id
let someSet = Set.of_list [SomeClass(1); SomeClass(2)]
let test = someSet.Contains(SomeClass(2))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个应该有效的实现:
Here's an implementation that should work:
我相信您需要实现
IComparer
才能使集合推导式(例如Set.of_list
)发挥作用。 (不是IComparable
,它的使用往往不太广泛 - 尽管我可能是错的。)这个 博客文章 一般性地解释了如何在 F# 中实现接口。 它还包括实现
IComparer
的类型的特定示例,这实际上并不像您希望的那样简单。让我知道这是否适合您。 我怀疑您实际上可能需要实现
IEqualityComparer
,因为据我所知,这就是 LINQ 集扩展方法的基础。 (在 BCL 中进行比较的所有这些接口确实令人困惑!)I believe you will need to implement
IComparer<T>
for set comprehensions (e.g.Set.of_list
) to work. (NotIComparable<T>
, which tends to be less widely used - though I may be wrong.)This blog post explains in general how to implement interfaces in F#. It also includes a specific example of a type implementing the
IComparer<T>
, which actually isn't straightforward as you might hope.Let me know if the works for you. I have some suspicion that you might in fact need to implement the
IEqualityComparer<T>
instead, since that's what LINQ set extension methods are based around, as far as I know. (It really gets confusing with all these interfaces for comparing in the BCL!)关于我的其他答案的评论,您可以将其纳入可重用的基类中,但我不确定这真的是一个好主意:
Regarding the comment on my other answer, you could factor this into a reusable base class, but I'm not sure it's really a good idea: