lambda表达式中的错误返回类型 [英] bad return type in lambda expression

查看:1216
本文介绍了lambda表达式中的错误返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在IntelliJ和Eclipse中编译良好,但JDK编译器1.8.0_25抱怨。首先是代码。

The following code compiles fine in IntelliJ and Eclipse, but the JDK compiler 1.8.0_25 complains. First, the code.

import java.util.function.Predicate;

public abstract class MyStream<E> {

  static <T> MyStream<T> create() {
    return null;
  }

  abstract MyStream<E> filter(MyPredicate<? super E> predicate);

  public interface MyPredicate<T> extends Predicate<T> {

    @Override
    boolean test(T t);
  }

  public void demo() {
    MyStream.<Boolean> create().filter(b -> b);
    MyStream.<String> create().filter(s -> s != null);
  }
}

javac 1.8.0_25的输出是:

The output from javac 1.8.0_25 is:

MyStream.java:18: error: incompatible types: incompatible parameter types in lambda expression
    MyStream.<Boolean> create().filter(b -> b);
                                       ^
MyStream.java:18: error: incompatible types: bad return type in lambda expression
    MyStream.<Boolean> create().filter(b -> b);
                                            ^
    ? super Boolean cannot be converted to boolean
MyStream.java:19: error: bad operand types for binary operator '!='
    MyStream.<String> create().filter(s -> s != null);
                                             ^
  first type:  ? super String
  second type: <null>
MyStream.java:19: error: incompatible types: incompatible parameter types in lambda expression
    MyStream.<String> create().filter(s -> s != null);
                                      ^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
4 errors

当我替换?超级E 只需 E ,JDK编译成功。

When I replace ? super E with simply E, JDK compiles successfully.

当我替换 filter(MyPredicate with filter(Predicate ),JDK编译成功。

When I replace filter(MyPredicate with filter(Predicate, JDK compiles successfully.

由于它适用于JDK 1.8.0_60,我怀疑这是一个编译器错误。

Since it works with JDK 1.8.0_60, I suspect it is a compiler bug.

有关导致此问题的原因及何时修复的详细信息?

Any details on what caused this and when it has been fixed?

推荐答案

如果lambda表达式出现在带通配符的目标类型中(在大多数情况下)

If a lambda expression appears in a target type with wildcards (as in most cases)

  Consumer<? super Boolean> consumer = b->{...}

问题出现了 - lambda表达式的类型是什么;特别是 b 的类型。

the question arises - what's the type of the lambda expression; in particular, the type of b.

当然,由于通配符,可能有很多选择;例如我们可以明确选择

Of course, there could be many choices due to the wildcards; e.g. we could explicitly choose

  Consumer<? super Boolean> consumer = (Object b)->{...}

但是,隐含地, b shoul d推断为布尔值。这是有道理的,因为消费者无论如何都应该只用布尔

However, implicitly, b should be inferred as Boolean. This makes sense since the consumer should only be fed with Boolean anyway.

http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.27.3


如果T是通配符参数化的函数接口类型且lambda表达式是隐式类型的,那么地面目标类型是T的非通配符参数化

If T is a wildcard-parameterized functional interface type and the lambda expression is implicitly typed, then the ground target type is the non-wildcard parameterization of T

(这可能假设通配符在目标类型上使用了正确的方差;如果假设不成立,我们可能会发现一些有趣的例子)

(This probably assumes that wildcards are use properly variance-wise on the target type; we might find some hilarious examples if the assumption doesn't hold)

这篇关于lambda表达式中的错误返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆