Java8和Vertx中的类型推断

时间:2019-03-01 09:39:05

标签: java-8 vert.x

根据文档,executeBlocking接口的Vertx方法具有以下签名:

<T> void executeBlocking(Handler<Future<T>> blockingCodeHandler, boolean ordered, Handler<AsyncResult<T>> resultHandler)

问题是,为什么(从形式上看)以下原因使Java无法推断Future<T>的通用类型(推断为Future<Object>):

    vertx.executeBlocking((deploymentFuture) -> {
        this.deploy();
        deploymentFuture.complete(new Boolean(true));
    }, (asyncResult) -> {
        start.complete();
    });

即使deploymentFuture被明确分配了Boolean。只有此显式调用才给出Java Future<Boolean>的概念:

    vertx.<Boolean>executeBlocking((deploymentFuture) -> {
        this.deploy();
        deploymentFuture.complete(new Boolean(true));
    }, (asyncResult) -> {
        start.complete();
    });

为什么在这种用例中推断类型这么麻烦?是因为类型不变(与ArrayList<Integer>ArrayList<Number>相似)。

基本上没有明确的呼叫类型,我被扔掉了

No instances of type variable T exist so that Future<T> conforms to Future<Object>

0 个答案:

没有答案