First of all, it is obvious that I don't understand the thenApply
well and that's why I am coming with a compiler error, but I tried to search and couldn't.
Here is my simple code:
import java.util.concurrent.CompletionStage;
public class Main5 {
public static void main(String[] args) {
}
static class Game {
public void doIt(int id) {
CompletionStage<Player> player = getPlayer(id).thenApplyAsync(p -> {
modifyPlayer(p.getId());
return getPlayer(p.getId());
});
}
private CompletionStage<Player> getPlayer(int id) {
//do http request to get the player info
}
private CompletionStage<Void> modifyPlayer(int id) {
//do http request to modify player's info
}
}
}
In the thenApplySync
I am getting this compiler error:
incompatible types: inference variable U has incompatible bounds
equality constraints: com.testapp.Player
lower bounds: java.util.concurrent.CompletionStage<com.testapp.Player>
The getPlayer
method returns a completion stage already, so what's wrong with that?
I know that
if I do:return getPlayer(p.getId()).toCompletableFuture().get();
it will work but I don't understand why. In my mind, i should return completion stage, not the object.
Help is appreciated.