这个隐含的 val 如何导致 StackOverFlowError?
(削减了我的原始代码,仍然导致错误)
object Complicit {
// a class with name, default, and conversion function as implicit val
case class CC[A](name: String, defaultValue: A)(implicit val convert: String => A) {
def getFrom(s: String): A= try {
convert(s)
} catch {
case t: Throwable =>
println("ERROR: %s".format(t)) // just to see the StackOverflowException
defaultValue
}
}
// this works fine
object Works {
val cc1= CC("first", 0.1)(_.toDouble)
}
// this causes java.lang.StackOverflowError due to the implicit
object Fails {
// !!! StackOverFlowError here
implicit val stringToDouble: String => Double= { _.toDouble }
val cc2= CC("second", 0.2)
}
def main(args: Array[String]) {
// this works
println("%s %f".format(Works.cc1.name, Works.cc1.getFrom("2.3")))
// this fails
println("%s %f".format(Fails.cc2.name, Fails.cc2.getFrom("4.5")))
}
}
我在做一些非法的隐含行为吗?