This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
package lambdatests; public class UnableToDeriveReturnType { void test4() { foo((paramToCheck) -> { if (paramToCheck != null) { return true; } else { return "String"; } }, "b"); } <T, R> void foo(PossibleFunctionalInterface<T, R> pfi, T paramToCheck) { R res = pfi.doSomething(paramToCheck); System.out.println(res.toString()); } interface PossibleFunctionalInterface<T, R> { R doSomething(T paramToCheck); } } Use anonymous inner class applied on the lambda specified in method test4 creates code: void test4() { foo(new PossibleFunctionalInterface<String, >() { public doSomething(String paramToCheck) { if (paramToCheck != null) { return true; } else { return "String"; } } }, "b"); } which is not compilable. I would expect that in this case the resulting code should be: void test4() { foo(new PossibleFunctionalInterface<String, Object>() { public Object doSomething(String paramToCheck) { if (paramToCheck != null) { return true; } else { return "String"; } } }, "b"); }
Surprisingly NetBeans can infer the suitable types in the following example: interface GenericTypesUsed<R, P extends R> { R doSomething(P param); } <R, P extends R> void genericTypesUsed(GenericTypesUsed<R, P> gtu, P param) { R res = gtu.doSomething(param); System.out.println(res.toString()); } void testGenericTypesUsed() { genericTypesUsed((ArrayList p) -> { if (System.currentTimeMillis() % 2 == 0) { return Collections.EMPTY_LIST; } else { return new java.util.Date(); } }, new ArrayList()); } => void testGenericTypesUsed() { genericTypesUsed(new GenericTypesUsed<Object, ArrayList>() { public Object doSomething(ArrayList p) { if (System.currentTimeMillis() % 2 == 0) { return Collections.EMPTY_LIST; } else { return new java.util.Date(); } } }, new ArrayList()); }
*** This bug has been marked as a duplicate of bug 254165 ***