我使用重载方法实现了一个Scala类,该方法可以使用Iterable[String]
或String*
varargs参数:
class StackOverflow(names: Iterable[String]) {
// This function creates a copy of the StackOverflow object
// copy is needed but this cannot be a case class.
private def copy(names: Iterable[String] = names) = new StackOverflow(names) // <- line 19
// overloaded methods
def withNames(names: Iterable[String]) = this.copy(names = names) // <- line 24
def withNames(names: String*) = require(names.nonEmpty); withNames(names.toIterable) // <- line 26
}
object App {
def main(args: Array[String]) = {
val x1 = new StackOverflow(Seq("a", "b"))
val x2 = x1.withNames("c", "d")
}
}
我希望x2
成为名为c
和d
的新对象,但由于无限递归导致StackOverflowError,因此无法创建值x2
: / p>
Exception in thread "main" java.lang.StackOverflowError
at scala.collection.LinearSeqLike$class.thisCollection(LinearSeqLike.scala:48)
at scala.collection.immutable.List.thisCollection(List.scala:84)
at scala.collection.immutable.List.thisCollection(List.scala:84)
at scala.collection.IterableLike$class.toIterable(IterableLike.scala:87)
at scala.collection.AbstractIterable.toIterable(Iterable.scala:54)
at test.StackOverflow.<init>(StackOverflow.scala:26)
at test.StackOverflow.copy(StackOverflow.scala:19)
at test.StackOverflow.withNames(StackOverflow.scala:24)
at test.StackOverflow.<init>(StackOverflow.scala:26)
at test.StackOverflow.copy(StackOverflow.scala:19)
at test.StackOverflow.withNames(StackOverflow.scala:24)
...
代码有什么问题?
答案 0 :(得分:6)
你被遗漏的牙箍困住了。
您甚至不需要val x2 = x1.withNames("c", "d")
行。
def withNames(names: String*) = require(names.nonEmpty); withNames(names.toIterable)
这实际上是:
def withNames(names: String*) = require(names.nonEmpty) // part of class
withNames(names.toIterable) // part of constructor
withNames(names.toIterable)
绝对正确,因为names
也是您班级中的一个字段。
因此,每当您实例化StackOverflow对象时,构造函数都会调用withNames()
来创建一个新实例,该实例会调用withNames()
,依此类推。要解决此问题,您必须在方法定义周围使用大括号。当然,既然你正在重载withNames(),你也必须指定返回类型。