0
votes

This code check if User exists and accordingly either returns the User or throws an exception

 def confirmSignupforUser(user:User):Future[User] = {
    val newInternalProfile = user.profile.internalProfileDetails.get.copy(confirmed=true)
    val newProfile = UserProfile(Some(newInternalProfile),user.profile.externalProfileDetails)
    val confirmedUser = user.copy(profile=newProfile)
    for(userOption <- userRepo.update(confirmedUser)) yield {
      userOption match {
        case Some(user) => user
        case None => throw UserDoesNotExistException("userNotExist",new Throwable("userNotExist"))
      }
    }
  }

If I call the code as follows, I get error non-variable type argument models.User in type pattern Option[models.User] is unchecked since it is eliminated by erasure for line modifiedUser:Option[User] <- if (userOption.isDefined) {confirmSignupforUser(userOption.get)} else Future.successful(None) Why?

       val result:Future[Result] = for{tokenOption:Option[UserToken] <- if(host != "" && redirectUrl != "" && successUrlParameter != "" && failUrlParameter != "") { userTokenRepo.findOne(UserTokenKey(UUID.fromString(token)))} else {Future.successful(None)}  
                                    userOption:Option[User] <- if (tokenOption.isDefined) {userRepo.findOne(tokenOption.get.userKeys)} else {Future.successful(None)} 
                                    modifiedUser:Option[User] <- if (userOption.isDefined) {confirmSignupforUser(userOption.get)} else Future.successful(None) 
                                    deletedToken:Option[UserTokenKey] <- if(modifiedUser.isDefined) {userTokenRepo.delete(UserTokenKey(UUID.fromString(token)))} else Future.successful(None)

       }
         yield { 
...
}
1
That code is almost unreadable... - Tim
sorry about that. I have worked on that. This is the old version. However, I found that some part of my old code isn't working with the new version. Thus I thought I'll raise a ticket to understand what is the issue. - Manu Chadha
Caan't you simplify to just show the important parts? Like remove the logs and format the code. - Luis Miguel Mejía Suárez
done. hopefully it is better now - Manu Chadha

1 Answers

0
votes

You are getting unchecked error because one of your if-else statement has wrong type as a result of a block.

NOTE: this method is returning Future[User]

def confirmSignupforUser(user:User):Future[User] = ???

but where you are getting compilation error is that else statement returning Future[Option[User]].
I would suggest to align this if else statement. You can do either change you method to return Future[Option[User]] OR you can change if part like this:

confirmSignupforUser(userOption.get).map(Some(_))

So, the broken line can be defined like this:

for {
    modifiedUser: Option[User] <- if (userOption.isDefined) {
            confirmSignupforUser(userOption.get).map(Some(_))
    } else Future.successful(None)
}

you can run it here as a simple demonstration

IMHO, it's better to go with OptionT here