简体   繁体   English

扩展CompletableFuture时类型不匹配

[英]Type mismatch when extending CompletableFuture

I am trying to extend CompletableFuture to do a thenCompose after handle , but I got a compiler error: 我试图延长CompletableFuturethenComposehandle ,但我得到一个编译器错误:

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: 作为记录,我试图隐藏此响应上使用的thenCompose ,基本上是:

.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> ? extends CompletableFuture<U> instead of ? extends U ? extends CompletableFuture<U>而不是? extends U ? extends U . ? extends U You could also accept as argument a more general CompletionStage instead of a CompletableFuture . 您也可以接受更通用的CompletionStage而不是CompletableFuture作为参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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