繁体   English   中英

扩展CompletableFuture时类型不匹配

[英]Type mismatch when extending CompletableFuture

我试图延长CompletableFuturethenComposehandle ,但我得到一个编译器错误:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM