In a project that I worked on in my guava library. I have something like this:
Optional< User > loginUser( ) {
User user = storage.get( request.id );
boolean success = ( user == null ) ?
false : user.password.equals( request.password );
return success == true ? Optional.of( user ) :Optional.absent ( );
}
and the compiler gives me an error:
Cannot drop from Optional <Object> to Optional <User>
More work here that works:
Optional< User > empty = Optional.absent ( );
return success == true ? Optional.of( user ) : empty;
How can I avoid creating an empty variable?
source
share