Domanda

I'm trying to write a function which will handle exception in future and returns new future, but I'm running into trouble and can't make sense of error message

scala> def composeHandlingFuture[T](fut: Future[T], default: T): Future[T] =
     | fut recover { case e: Exception => default }
<console>:19: error: type mismatch;
 found   : default.type (with underlying type A1 => B1)
 required: T
       fut recover { case e: Exception => default }
                                          ^

Isn't default.type equal to T as signature requires? How does it have anything to do with type A1 => B1?

Any help appreciated.

P.S. I'm using Scala 2.10.1

È stato utile?

Soluzione

The problem comes from typer phase. If we use -Xprint:typer then we can see the problem:

def composeHandlingFuture[T >: Nothing <: Any](fut: scala.concurrent.Future[T], default: T): scala.concurrent.Future[T] = fut.recover[T](({
      @SerialVersionUID(0) final <synthetic> class $anonfun extends scala.runtime.AbstractPartialFunction[Throwable,T] with Serializable {
        /////// Init method //////

        final override def applyOrElse[A1 >: Nothing <: Throwable, B1 >: T <: Any](x1: A1, default: A1 => B1): B1 = ((x1.asInstanceOf[Throwable]: Throwable): Throwable @unchecked) match {
          case (e @ (_: Exception)) => <error: value default> // your argument comes here, but name clash happens
          case (defaultCase$ @ _) => default.apply(x1)
        };


        //// IsDefinedAt method ////
  }

The problem is in PartialFunction, second argument to aplyOrElse is also called default with type A1 => B1. Here is it in the Scala compiler Typer phase, so you can make a ticket i guess

Altri suggerimenti

Strange, as soon as I renamed default variable, everything compiled fine:

scala> def composeHandlingFuture[T](fut: Future[T], x: T): Future[T] =
     | fut recover { case e: Exception => x }
composeHandlingFuture: [T](fut: scala.concurrent.Future[T], x: T)scala.concurrent.Future[T]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top