Scala警告类型推断为“ AnyVal”

时间:2018-07-23 09:14:54

标签: scala warnings type-inference

我有这个演员,带有下面的接收代码:

override def receive: Receive = {

case RefreshAccessTokenRequest(accountId, Some(refreshToken), _, _) =>
  concurApi.refreshAccessToken(accountId, refreshToken) onComplete {
    case Success(refreshTokenResult) =>
      context.parent ! ConcurResultCallbackInformation(accountId, Right(refreshTokenResult))
      accountService.updateAccountOAuth2Credentials(accountId, refreshTokenResult.toOAuth2Credentials) recover {
        case ex => logger.error("Error updating account Id: " + accountId + " with new refresh token. message: " + ex.getMessage)
      }
      context.stop(self)
    case Failure(t) =>
      context.parent ! ConcurResultCallbackInformation(accountId, Left(t))
      context.stop(self)
  }

case Cancel => context.stop(self)

case x => logger.error(s"ConcurAccessTokenActor: Got an unknown message $x")

}

我在粗体部分收到警告:

警告:(18,42)推断类型为AnyVal;这可能表示编程错误。         成功案例(refreshTokenResult)=>

这是refreshAccessToken函数:

def refreshAccessToken(accountId: String, refreshToken: String)(implicit context: ExecutionContext) : Future[ConcurRefreshTokenResult] = {
val body = s"client_id=${sandboxClientId}"

val futureRes = ws.url(sandboxRefreshTokenFullUrl).withHeaders("Content-Type" -> "application/x-www-form-urlencoded").post(body)
futureRes.flatMap { res =>
  res.status match {
    case 200 =>
      val extract = Try {
        res.body.fromJson[ConcurRefreshTokenResult]
      }.recover { case ex =>
        throw ex
      }
      Promise.fromTry(extract).future
    case 400 =>
      Future.failed(new RuntimeException(s"refresh Access Token failed with status 400, and body: ${res.body}"))
    case x =>
      Future.failed(new RuntimeException(s"refresh Access Token failed with status $x, and body: ${res.body}"))
  }
}
}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

好吧,我明白了。

警告来自此代码部分:

case Success(refreshTokenResult) =>
  context.parent ! ConcurResultCallbackInformation(accountId, Right(refreshTokenResult))
  accountService.updateAccountOAuth2Credentials(accountId, refreshTokenResult.toOAuth2Credentials) recover {
    case ex => logger.error("Error updating account Id: " + accountId + " with new refresh token. message: " + ex.getMessage)
  }
  context.stop(self)

'updateAccountOAuth2Credentials'函数返回Future [Boolean],但是恢复部分返回Unit,因此将其插入AnyVal

我认为唯一令人困惑的是警告箭头的位置,因为此函数出现在其他两个函数之间的中间,并且对整个case块的返回类型没有影响