In the second code snippet
new Test<>(new Object(){ public String text = "Something"; }, (o) -> { System.out.println(o.text); });
it compiles because an argument of type Test to call the constructor is output (since the diamond operator is used), and it is inferred to an anonymous type, which evaluates the first argument (the type of the anonymous class) and, therefore, the second type of the argument - operation<that anonymous class type> , which is working.
In the first code snippet, the expression
t.runOperation((o) -> { System.out.println(o.text); // text cannot be resolved })
not compiled. Here, the lambda type is inferred based on the type of the variable t , which is Test<?> . Thus, the runOperation argument must be operation<some unknown type> . The only runOperation argument that will work here is null .
source share