小编Cor*_*ore的帖子

scala隐式导致StackOverflowError

这个隐式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 …
Run Code Online (Sandbox Code Playgroud)

stack-overflow scala implicits

7
推荐指数
1
解决办法
420
查看次数

标签 统计

implicits ×1

scala ×1

stack-overflow ×1