The used method reference has return type Integer. But an incompatible String is allowed in the following example.
How to fix the method <
Its the type inference that is playing its role here. Consider the generic R in the method signature:
Builder with(Function getter, R returnValue)
In the case as listed:
Builder.of(MyInterface.class).with(MyInterface::getLength, "I am NOT an Integer");
the type of R is successfully inferred as
Serializable, Comparable extends Serializable & Comparable>>
and a String does imply by this type, hence the compilation succeeds.
To explicitly specify the type of R and find out the incompatibility, one can simply change the line of code as :
Builder.of(MyInterface.class).with(MyInterface::getLength, "not valid");