【问题标题】:Mixin SynchronizedSet with SortedSet having implicit Ordering objectMixin SynchronizedSet 与 SortedSet 具有隐式排序对象
【发布时间】:2013-04-04 10:58:27
【问题描述】:

我似乎无法创建一个也混入 SynchronizedSet 的 SortedSet。问题的症结在于 SortedSet 需要一个隐式的 Ordering 对象。

val orderByIdThenName = Ordering[(Int, String)].on[Foo](foo => foo.id -> foo.name)
new mutable.TreeSet[Foo]()(orderByIdThenName) // <- Works fine and is Ordered
new mutable.HashSet[Foo] with mutable.SynchronizedSet[Foo] // <- Mixin works
new mutable.TreeSet[Foo]()(orderByCount) with mutable.SynchronizedSet[Foo] // Fail!

最后一行给出错误“无法创建对象,因为 scala.collection.SortedSetLike 中的成员 Ordering[A] 未定义。

有什么建议吗?

【问题讨论】:

    标签: scala collections implicit


    【解决方案1】:

    这看起来是 IntelliJ 中的一个错误。我能够重现该问题并在编辑器中看到错误,但编译时没有错误或警告。

    因为没有给出 orderByCount 的定义,我假设它是这样的:

    val orderByCount = Ordering[Int].on[Foo](_.count)
    new mutable.TreeSet[Foo]()(orderByCount) with mutable.SynchronizedSet[Foo]
    

    更新: 想出了一种方法来通过覆盖ordering 来消除 IntelliJ 中的错误:

    new mutable.TreeSet[Foo]()(orderByCount) with mutable.SynchronizedSet[Foo] {
        implicit override val ordering: Ordering[Foo] = orderByCount
    }
    

    【讨论】:

    • 效果很好,谢谢。是的,它看起来像一个 IntelliJ 错误。
    猜你喜欢
    • 1970-01-01
    • 2016-01-23
    • 2018-06-04
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多