在以下代码中,我得到一个令牌,该令牌正在查询返回Future
的多个数据库。我想根据上一个查询的结果执行下一个查询。
val result:Future[Result] = for{tokenOption:Option[UserToken] <- if(urlHost != "" && successUrlParameter != "" && failUrlParameter != "") {userTokenRepo.findOne(UserTokenKey(UUID.fromString(token)))} else {Future.successful(None)} //generator 1 - get token from database
userOption:Option[User] <- if (tokenOption.isDefined) {userRepo.findOne(tokenOption.get.userKeys)} else {Future.successful(None)} //generator2. found token, look for corresponding user to which the token belongs
modifiedUser:Option[User] <- if (userOption.isDefined) {confirmSignupforUser(userOption.get)} else Future.successful(None) //generator 3. found user and token. Update profile
deletedToken:Option[UserTokenKey] <- if(modifiedUser.isDefined) {userTokenRepo.delete(UserTokenKey(UUID.fromString(token)))} else Future.successful(None)
}
yield { //check if we have user and token and modified user here. If any is missing, return error else success
if(urlHost == "" || successUrlParameter == "" || failUrlParameter == ""){//error in reading redirection url.
InternalServerError(Json.toJson(JsonResultError("Internal Server Error. Redirection missing ")))
} else { //read redirection url, so can redirect with success or failure parameter
println("db query results tokenOption: " + tokenOption + ", userOption: " + userOption + " : modifiedUserOption: " + modifiedUser + ", deletedToken: " + deletedToken)
//all db ops other than deletinng token should be successful. Error in deletinng token is not a critical error from user perspective and thus should not affect signup verification
if (tokenOption.isDefined && userOption.isDefined && modifiedUser.isDefined /*&& deletedToken.isDefined*/) {
Redirect(s"${urlHost};${successUrlParameter}") //TODOM - pick from config
}
else {
/*TODOM - when redirecting with error, can provide additional info why sign up failed*/
Redirect(s"${urlHost};${failUrlParameter}") //TODOM - pick from config
}
}
}
我在编译器选项中启用了"-Xfatal-warnings"
和-Ywarn-unused:locals
。我收到警告,未使用tokenOption
,userOption
,modifiedUser
和deletedToken
,但我正在使用它们(如果返回Option
,则进行下一个查询之前的isDefined
。)。为什么我会收到编译器警告?
Error:(410, 40) pattern var tokenOption in value $anonfun is never used; `tokenOption@_' suppresses this warning
val result:Future[Result] = for{tokenOption:Option[UserToken] <- if(urlHost != "" && successUrlParameter != "" && failUrlParameter != "") {userTokenRepo.findOne(UserTokenKey(UUID.fromString(token)))} else {Future.successful(None)} //generator 1 - get token from database
Error:(411, 37) pattern var userOption in value $anonfun is never used; `userOption@_' suppresses this warning
userOption:Option[User] <- if (tokenOption.isDefined) {userRepo.findOne(tokenOption.get.userKeys)} else {Future.successful(None)} //generator2. found token, look for corresponding user to which the token belongs
答案 0 :(得分:3)
根据Unused warning occurred even if the variable was used #11175,这似乎是一个错误。以下代码段
object Hello {
for {
a <- Option(1)
b <- Option(2) if a == 1
} yield a + b
}
在使用Scala 2.12.8和2.13的计算机上重现了该问题,但是在Scala 2.12.1中似乎没有出现此问题,因此,我建议尝试使用不同的版本,然后看看哪个版本适合您。