【问题标题】:Scala warning a type was inferred to be 'AnyVal'Scala 警告类型被推断为“AnyVal”
【发布时间】:2018-07-23 09:14:53
【问题描述】:

我有这个演员,接收代码如下:

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;这可能表示编程错误。 case Success(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}"))
  }
}
}

有什么想法吗?

【问题讨论】:

  • 你的第 18 行是哪一行?
  • .info.stop的返回类型是什么?
  • 第 18 行:case Success(refreshTokenResult) => 。注意'=>'箭头上的箭头点
  • .info(你的意思是 .error)和 .stop 都是 UNIT 类型。我想我找到了原因……“updateAccountOAuth2Credentials”函数返回 Future[Boolean] 所以整个块的返回类型是 AnyVal。

标签: scala warnings type-inference


【解决方案1】:

好的,我明白了..

警告来自此代码部分:

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 块的返回类型没有影响

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多