Cats Monad Future


#1

Looking for help here as i do not understand why this code causes runtime error

import cats.Monad
import cats.instances.future._
import scala.concurrent._
import scala.concurrent.duration._

import scala.concurrent.ExecutionContext.Implicits.global

val fm = Monad[Future]

Await.result(
  fm.flatMap(fm.pure(1)) {
    x => fm.pure(x+2)
  }, 1.second
)
java.lang.NoClassDefFoundError: Could not initialize class $line9.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$
        at scala.concurrent.Future.$anonfun$flatMap$1(Future.scala:302)
        at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
        at scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:140)
        at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
        at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
        at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
        at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
java.util.concurrent.TimeoutException: Futures timed out after [1 second]
  at scala.concurrent.impl.Promise$DefaultPromise.ready(Promise.scala:255)
  at scala.concurrent.impl.Promise$DefaultPromise.result(Promise.scala:259)
  at scala.concurrent.Await$.$anonfun$result$1(package.scala:215)
  at scala.concurrent.BlockContext$DefaultBlockContext$.blockOn(BlockContext.scala:53)
  at scala.concurrent.Await$.result(package.scala:142)
  ... 43 elided

#2

This is not really related to Cats. A more self contained reproduction would be

scala> import scala.concurrent._, duration._, ExecutionContext.Implicits.global
import scala.concurrent._
import duration._
import ExecutionContext.Implicits.global

scala> Await.result(Future(42), 1.second)
java.lang.NoClassDefFoundError: Could not initialize class $line49.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$

This seems to be a known bug (see comment from March 1).