In the following code, I get a token I am querying multiple databases which return Future
. I want to do the next query based on the outcome of the previous one.
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
}
}
}
I have enabled "-Xfatal-warnings"
and -Ywarn-unused:locals
in compiler options. I am getting warning that tokenOption
, userOption
,modifiedUser
and deletedToken
are not used but I am using them (the next query is done if the Option
returned by previous one isDefined
.). Why am I getting the compiler warning?
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
This seems to be a bug according to Unused warning occurred even if the variable was used #11175. The following snippet
object Hello {
for {
a <- Option(1)
b <- Option(2) if a == 1
} yield a + b
}
reproduces the issue on my machine with Scala 2.12.8 and 2.13, but does not seem to occur with Scala 2.12.1, so I would suggest trying different versions and see which one works for you.