- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试解决缺少 collection.mutable.SortedSet
这就是我的跳过列表实现的问题。我快到了:
import collection.{SortedSet => CSortedSet, SortedSetLike => CSortedSetLike}
import collection.mutable.{Set => MSet, SetLike => MSetLike}
import collection.generic.{MutableSetFactory, GenericCompanion}
object SkipList extends MutableSetFactory[SkipList] {
def empty[A](implicit ord: Ordering[A]): SkipList[A] = ???
}
trait SkipList[A] extends MSet[A] with MSetLike[A, SkipList[A]] with CSortedSet[A]
with CSortedSetLike[A, SkipList[A]] {
override def empty: SkipList[A] = SkipList.empty[A](ordering)
def rangeImpl(from: Option[A], until: Option[A]): SkipList[A] =
throw new Exception("Unsupported operation")
}
好的,编译成功了。但与不可变的排序集不同,我可以明确地做到这一点
case class Holder(i: Int) extends Ordered[Holder] {
def compare(b: Holder) = i.compare(b.i)
}
def test1(iss: ISortedSet[Holder]) = iss.map(_.i)
test1(ISortedSet(Holder(4), Holder(77), Holder(-2))).toList
这对我的可变排序集失败:
def test2(sl: SkipList[Holder]) = sl.map(_.i)
与
error: ambiguous implicit values:
both method canBuildFrom in object SortedSet of type [A](implicit ord: Ordering[A])scala.collection.generic.CanBuildFrom[scala.collection.SortedSet.Coll,A,scala.collection.SortedSet[A]]
and method canBuildFrom in object Set of type [A]=> scala.collection.generic.CanBuildFrom[scala.collection.mutable.Set.Coll,A,scala.collection.mutable.Set[A]]
match expected type scala.collection.generic.CanBuildFrom[SkipList[Holder],Int,That]
def test2( sl: SkipList[ Holder ]) = sl.map( _.i )
^
这超出了我的概述范围。关于如何实现不可变排序集已经完成的任何线索?有没有机会消除这种歧义?
最佳答案
看起来您需要在对象 SkipList
中定义一个 canBuildFrom
方法。虽然这样做不是必需的,但定义您自己的 canBuildFrom
的目的是确保继承的方法返回可能的最佳类型。由于您混合了几个特征,如果您没有定义自己的隐式 canBuildFrom
,就会产生歧义。
在你的情况下,添加类似的东西,
import collection.generic.{CanBuildFrom, MutableSetFactory}
import collection.mutable.{Set, SetLike}
object SkipList extends MutableSetFactory[SkipList] {
implicit def canBuildFrom[A : Ordering]: CanBuildFrom[Coll, A, SkipList[A]] =
new CanBuildFrom[Coll, A, SkipList[A]] {
def apply(from: Coll) = newBuilder[A]
def apply() = newBuilder[A]
}
}
trait SkipList[A]
extends Set[A] with SetLike[A, SkipList[A]] {
override def empty: SkipList[A] = SkipList.empty[A]
}
应该可以解决问题。
几个月前,Martin 写了一篇关于在 The Architecture of Scala Collections 中实现自定义集合的好文档。 ,其中包括 section on integrating new sets and maps .虽然很难找到,但如果您有兴趣建立自己的收藏,它是一种权威资源。
关于scala - 创建 `collection.mutable.SortedSet` -- `map` 方法中的歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6763588/
虽然我正在运行以测试此 thread 中的一些代码我发现了一件奇怪的事情,如果你考虑下面的程序 import java.util.ArrayList; import java.util.List; p
我只是很好奇为什么使用 using 指令以这种方式设计它。对于 1) struct 被视为命名空间,对于 2) 它不是: struct foo { using type0 = int; }; na
我需要在 ANTLR 中匹配一条消息,该消息包含由/分隔的 2 个字段第一个字段可以有 1..3 数字,第二个字段可以有 1..2 数字, 这不起作用 msg: f1 '/' f2; f1: DIGI
我想定义一个带有 x, y, z 坐标的 3D 矢量。可以使用(+)运算符添加向量,并可以使用length函数计算长度 如果我想编译它,我会收到以下错误: It could refer to eith
所以,今天我一直在测试 Java 的重载技术,但遇到了我无法解释的歧义。基本上,当存在带有原语及其相应包装器的可变参数方法时,编译器会提示并且无法决定选择哪一个,我不明白为什么?人类很容易决定而不是编
发现: Ambiguity while overloading the cast operator 和 C++ Operator Ambiguity 没有帮助 情况: 在容器类的内部我有: opera
我正在使用以下 XPath 片段 ancestor::contribution[1]/preceding-sibling::contribution[@speaker-reference][1] 我的
在尝试依赖可变参数模板实现一些事情时,我偶然发现了一些我无法解释的事情。我将问题归结为以下代码片段: template struct A {}; template class Z, typenam
我最近对 LALR 的研究足以写一篇 LALR generator ,并且我正在尝试为其构造一个 java 或 c# 风格的语法(其开头指定为 here )。 我知道编写解析器生成器需要额外的努力
在阅读 wikipedia article关于修订控制的文章,我发现了一些似乎定义不明确的术语,并且想知道它们是如何在现实世界中实际使用/应用的。具体来说: “主线”与“基线” “分支”与“流” “结
我有以下代码: class A{ public void print(A a){ System.out.println("A 1"); } } class B exte
考虑我试过的以下查询,有两个表,Orders 和 Customers,每个表都有列名称 CustomerID,当我尝试显示两个 CustomerID 时,只有一列显示,我不明白为什么会这样,或者我是理
我正在为基本汇编语言编写一个简单的组合词法分析器/解析器。我的问题是,在解析操作码时,我需要解析一些十六进制,它是指令计数器,可能是立即值等,以及实际的操作码。 基本上,当解析像 add 这样的东西时
我正在尝试在 mysql 中跨两个不同的数据库进行标题搜索,以匹配来自不同来源的数据。在 db1 或 db2 中,标题有时会在一个数据库中以“第一个标题”开头,而在另一个数据库中则以“第一个标题”开头
我对 token 级别的歧义有疑问。 问题看起来是这样的。我的代码看起来像这样,因此标记 t_UN1 具有更高的优先级。 t_ignore = ' \t\v\r' # whitespace ....
在 Main() 的最后一行,我在尝试执行指针操作时遇到错误,但我实际上想做的是调用我在 Foo 类中定义的乘法运算符。如果这不是调用乘法的语法,那是什么? namespace test {
在此page它说: The "00" specifier causes the value to be rounded to the nearest digit preceding the decim
我正在使用 C++ Builder 并使用 TDateTime 数据类型创建 GUI 应用程序。 当我在 TDateTime 对象上使用 MonthOf() 方法时,我得到的值比 TDateTime
$7.3.3/14 (C++03) struct A { int x(); }; struct B : A { }; struct C : A { using A::x; int x(in
我一直在开发一种适配器类,当我在 clang 下遇到问题。当定义了左值引用和右值引用的转换运算符时,您会在尝试从您的类中移出时遇到歧义编译错误(当这样的代码应该没问题时,如 operator cons
我是一名优秀的程序员,十分优秀!