scalascala-repl

Why does following throw exception in Scala REPL?


Why does last line of following piece of code throw error in 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)

But when I create a main class and execute the same code it works fine:

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 ")
  }
}

Output:

v1=42, v2=44


Solution

  • This bug (including your exact source) is being tracked in this bug against scala:

    https://github.com/scala/bug/issues/9076#issuecomment-292435527