简体   繁体   English

Scala类型不匹配:default.type(基础类型A1 => B1)

[英]Scala type mismatch: default.type (with underlying type A1 => B1)

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? default.type是否等于T作为签名要求? How does it have anything to do with type A1 => B1 ? 它与type A1 => B1有什么关系?

Any help appreciated. 任何帮助赞赏。

PS I'm using Scala 2.10.1 PS我正在使用Scala 2.10.1

The problem comes from typer phase. 问题来自于typer阶段。 If we use -Xprint:typer then we can see the problem: 如果我们使用-Xprint:typer,那么我们可以看到问题:

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 . 问题出在PartialFunction中,aplyOrElse的第二个参数也称为default ,类型为A1 => B1 Here is it in the Scala compiler Typer phase, so you can make a ticket i guess 是在Scala编译器Typer阶段,所以你可以制作一张票

Strange, as soon as I renamed default variable, everything compiled fine: 奇怪的是,一旦我重命名default变量,一切编译得很好:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM