为什么以下代码段的最后一行在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
这个错误(包括你的确切来源)正在针对scala的这个错误中被跟踪:
https://github.com/scala/bug/issues/9076#issuecomment-292435527