除非指定参数类型,否则Java无法编译通用lambda参数

use*_*791 7 java generics type-inference

在测试时,我将Junit升级到5.0(从而用新版本替换了一些assertTrue()方法)。这样做之后,我发现我的测试之一没有编译。我将问题简化为没有junit或其他依赖项的普通Java。结果是以下代码无法编译:

  public static void recreate() {
    // This does NOT work
    Recreation.assertTrue(identity((x) -> Boolean.TRUE)); 
    // This DOES work
    Recreation.assertTrue(identity((String x) -> Boolean.TRUE)); 
  }

  private static class Recreation {
    public static void assertTrue(boolean b) {
      System.out.println("boolean argument: " + b);
    }

    // If this method is removed, the code will compile. 
    public static void assertTrue(Supplier<Boolean> booleanSupplier) {
      System.out.println("supplier argument: " + booleanSupplier.toString());
    }
  }

  private static <K> K identity(Function<String, K> function) {
    return function.apply("hello");
  }
Run Code Online (Sandbox Code Playgroud)

如以上示例所示,如果满足以下任一条件,则代码将编译:

  1. 指定了lambda参数类型

  2. 重载的as​​sertTrue(Supplier booleanSupplier)方法已删除

这是类型推断/擦除的问题,还是这里发生了什么?

生成错误:

Error:(10, 35) incompatible types: inference variable K has incompatible bounds
    lower bounds: java.util.function.Supplier<java.lang.Boolean>,java.lang.Object
    lower bounds: java.lang.Boolean
Run Code Online (Sandbox Code Playgroud)

眼镜:

openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment (build 11.0.1+13-Ubuntu-3ubuntu114.04ppa1)
OpenJDK 64-Bit Server VM (build 11.0.1+13-Ubuntu-3ubuntu114.04ppa1, mixed mode, sharing)

OS: Ubuntu 14.04.5 LTS
Run Code Online (Sandbox Code Playgroud)

编辑:确认问题也存在于Java 8上:

java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
exit status 1
Main.java:10: error: incompatible types: inferred type does not conform to upper bound(s)
    Recreation.assertTrue(identity((x) -> Boolean.TRUE));
                                  ^
    inferred: Boolean
    upper bound(s): Supplier<Boolean>,Object
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
Run Code Online (Sandbox Code Playgroud)

小智 4

在浏览并阅读此处的 Java 语言规范之后https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2.1

我认为这里有两个步骤:

首先,重载解析无法推断 的类型,identity((x) -> Boolean.TRUE)因为它是隐式 lambda,我认为为了简单起见,没有考虑到它。因此,它将扩大参数的搜索和使用public static void assertTrue(Supplier<Boolean> booleanSupplier)

其次,重载解析完成后,类型推断开始。这次它真正检查推断的类型,即 a Boolean,并且因为它与Supplier<Boolean> booleanSupplier,因此您会收到编译错误。

就像之前的答案一样,有解决方案,

例如

Recreation.assertTrue(identity((x) -> () -> Boolean.TRUE));
Run Code Online (Sandbox Code Playgroud)

我在这里找到了一个很好的解释:Java8: lambdas 和重载方法的歧义