I am trying to extend CompletableFuture
to do a thenCompose
after handle
, but I got a compiler error:
Type mismatch: cannot convert from CompletableFuture(Object) to CompletableFuture(U)
This is my code:
public class MyCompletableFuture<T> extends CompletableFuture<T> {
public <U> CompletableFuture<U> handleAndCompose(BiFunction<? super T, Throwable, ? extends U> fn) {
return super.handle(fn).thenCompose(x->x);
}
}
For the record, I am trying to hide the thenCompose
used on this reponse which is basically:
.handle((x, t) -> {
if (t != null) {
return askPong("Ping");
} else {
return x;
}
)
The signature of your method is incorrect. It should be:
public <U> CompletableFuture<U> handleAndCompose(BiFunction<? super T, Throwable, ? extends CompletableFuture<U>> fn) {
return super.handle(fn).thenCompose(x->x);
}
Note that the function given returns ? extends CompletableFuture<U>
? extends CompletableFuture<U>
instead of ? extends U
? extends U
. You could also accept as argument a more general CompletionStage
instead of a CompletableFuture
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.