1

我似乎无法创建一个也混入 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] 未定义。

有什么建议么?

4

1 回答 1

0

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

因为没有给出 orderByCount 的定义,所以我假设它类似于:

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

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

new mutable.TreeSet[Foo]()(orderByCount) with mutable.SynchronizedSet[Foo] {
    implicit override val ordering: Ordering[Foo] = orderByCount
}
于 2013-04-12T03:45:01.880 回答