与andThen匹配

时间:2017-07-19 10:55:11

标签: scala

我的(不完整)代码如下所示:

  def doSomething(name: String): { implicit request =>
    check(name).map {
      case Something() => Ok()
      case SomethingElse() => NoContent
    }
    .andThen {
      case r: Result => logger.info(s"Request finished with status ${r.header.status}")
    }
  }

我收到这样的错误:

[error] myfile.scala:39: fruitless type test: a value of type scala.util.Try[play.api.mvc.Result] cannot also be a play.api.mvc.Result
[error]       case r: Result => logger.info(s"Request finished with status ${r.header.status}")

如何匹配Try

1 个答案:

答案 0 :(得分:3)

如果您尝试匹配Try,则需要匹配其构造函数,而不是其构造函数所匹配的内容:

.andThen {
    case Success(r) =>
        logger.info(s"Request finished with status ${r.header.status}")

    case Failure(e) => // Handle failure
}