我有两种方法可以将元素添加到自定义集合中:
--allow-shlib-undefined
--no-allow-shlib-undefined
Allows (the default) or disallows undefined symbols in shared
libraries (It is meant, in shared libraries _linked_against_, not the
one we're creating!--Pavel Shved). This switch is similar to --no-un-
defined except that it determines the behaviour when the undefined
symbols are in a shared library rather than a regular object file. It
does not affect how undefined symbols in regular object files are
handled.
The reason that --allow-shlib-undefined is the default is that the
shared library being specified at link time may not be the same as
the one that is available at load time, so the symbols might actually
be resolvable at load time. Plus there are some systems, (eg BeOS)
where undefined symbols in shared libraries is normal. (The kernel
patches them at load time to select which function is most appropri-
ate for the current architecture. This is used for example to dynam-
ically select an appropriate memset function). Apparently it is also
normal for HPPA shared libraries to have undefined symbols.
它会引发Stackoverflow异常,因为对class WrappedMap[A, B] {
protected var a2b = Map[A, B]()
def +=(a: A, b: B) = {
a2b += a -> b
this
}
def +=(t: (A,B)): this.type = {
this.+=(t._1, t._2)
}
def ++=(t: Iterable[(A,B)]) = {
(this /: t){ case (b, elem) => b += elem }
}
}
val c = new WrappedMap[Int, Int]
c ++= Seq((1, 2),(2, 4))
内+=
的调用会调用++=
,而+=
会调用自身而不是调用第一个变体。
如何更改第二个+=
正文中的通话,以便它调用第一个?
答案 0 :(得分:1)
问题是this.type
的规范是+=
的第二种形式的返回类型。不确定确切原因,但编译器不能将第一个表单识别为返回this.type
,因此不能用它来满足第二个表单的返回类型。我重新创建了它:
case class Foo[A,B](s: String) {
def bar(a: String, b: String) = {
this.copy(s"a: $a b: $b")
}
def bar(c: (String, String)): this.type = {
this.bar (c._1, c._2)
}
}
val f = Foo("")
f bar (("hello", "world"))
相反,您只需使用类并输入params作为返回类型:
case class Foo[A, B](s: String) {
def bar(a: String, b: String) : Foo[A,B] = {
this.copy(s"a: $a b: $b")
}
def bar(c: (String, String)): Foo[A, B] = {
this.bar (c._1, c._2)
}
}
val f = Foo[Int, Long]("")
f bar (("hello", "world"))
或者,需要支持继承的地方:
class Foo[A, B] {
private var s: String = ""
def bar(a: String, b: String) : this.type = {
s = s"a: $a b: $b"
this
}
def bar(c: (String, String)): this.type = {
this.bar (c._1, c._2)
}
}
case object Foo2 extends Foo[Int, Long]
Foo2 bar (("hello", "world"))