[英]Type mismatch when extending CompletableFuture
我試圖延長CompletableFuture
做thenCompose
后handle
,但我得到一個編譯器錯誤:
Type mismatch: cannot convert from CompletableFuture(Object) to CompletableFuture(U)
這是我的代碼:
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);
}
}
作為記錄,我試圖隱藏此響應上使用的thenCompose
,基本上是:
.handle((x, t) -> {
if (t != null) {
return askPong("Ping");
} else {
return x;
}
)
您的方法簽名不正確。 它應該是:
public <U> CompletableFuture<U> handleAndCompose(BiFunction<? super T, Throwable, ? extends CompletableFuture<U>> fn) {
return super.handle(fn).thenCompose(x->x);
}
注意給定的函數返回? extends CompletableFuture<U>
? extends CompletableFuture<U>
而不是? extends U
? extends U
您也可以接受更通用的CompletionStage
而不是CompletableFuture
作為參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.