The reason why it works using method reference is obvious: method references include both the parameters and return types, so it gives the compiler much more information than needed. As Piet said, specifying the argument type for the mapper function is enough to make type inference work.
You have in fact many possible solutions:
1) using a method reference
MyObj::getNum
2) specifying the parameter type for the mapper:
(MyObj obj2) -> obj2.getNum()
3) using a parameterized method call
: Collectors.maxBy(Comparator.<Double>naturalOrder())
4) Using a named
Function:
5) Using an named
Comparator for the downstream
Collector:
Thee are probably many other possibilities to help the compiler inferring the
Double type. The best one is obviously using method references.
This said, your very question was "is it a JDK bug?". The answer is no. It might be an Eclipse bug, depending on what were the requirements. Eclipse has its own compiler, and this compiler is (as you noted) not fully compatible with Java. So if the requirement for the Eclipse compiler was to be compatible with Java, it's an Eclipse bug. If the requirement was to be compatible with the Java 8 specification only, it's a feature.
Not everything is specified. For what is not specified, there is a "reference implementation". From
Java Platform, Standard Edition 8 Reference Implementations :
The official Reference Implementations for Java SE 8 (JSR 337) are based solely upon open-source code available from the JDK 8 Project in the OpenJDK Community.
About one year ago, I submitted an issue to the Eclipse development team about the inverse problem: places were the Eclipse compiler was unable to infer types although Oracle JDK and OpenJDK could. The issue was rejected because they said it was not Eclipse which was not compatible, it was Oracle JDK that was not conform to the spec.
This obviously was not a big problem. An
IDE with restrictions is fine. But an IDE that allows more that what the compiler used in production can accept is much more problematic. This shows that in any case, programmers should either avoid using Eclipse or compile with the JDK used in the build chain before submitting their work.
This, by the way, is not a problem specific to Eclipse. Programmers should develop with the same version of the JDK that the one used in production. Or at least, they should compile their code and run their tests locally using the correct version before submitting.