Java cannot compile generic lambda argument unless parameter type is specified
After looking around and reading the Java Language Specification here https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2.1
I think there are two steps here:
First, the overloading resolution couldn't infer the type of identity((x) -> Boolean.TRUE)
because it's implicit lambda, I think it's not taken into account for simplicity sake. Thus, it will widen the parameter search and use public static void assertTrue(Supplier<Boolean> booleanSupplier)
.
Second, after overloading resolution is done, type inference kicks in. This time it really check the inferred type which is a Boolean
, and since it's not compatible with Supplier<Boolean> booleanSupplier
, you get the compilation error.
Like previous answer, there are solutions to this,
e.g
Recreation.assertTrue(identity((x) -> () -> Boolean.TRUE));
I found a good explanation here: Java8: ambiguity with lambdas and overloaded methods