为什么下面的代码的最后一行在scala REPL中引发错误?
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
// This is okay
val futureInt = Future[Int] { 42 }
val v1 = Await.result(futureInt, 1.second)
// This throw error: java.lang.NoClassDefFoundError: Could not initialize class $line8.$read$$iw$$iw$$iw$$iw$$iw$$iw$
val v2 = Await.result(Future[Int]{ 42 }, 1.second)
但是当我创建一个主类并执行相同的代码时,它可以正常工作:
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
object Main {
def main(args: Array[String]): Unit = {
val futureInt = Future[Int] { 42 }
val v1 = Await.result(futureInt, 1.second)
val v2 = Await.result(Future[Int] { 44 }, 1.second)
println(s"v1=$v1, v2=$v2 ")
}
}
输出:
v1 = 42,v2 = 44
答案 0 :(得分:1)
此bug(包括您的确切来源)已在针对scala的此bug中进行跟踪:
https://github.com/scala/bug/issues/9076#issuecomment-292435527