Issue with Comparator.comparing

B. Blaser bsrbnd at gmail.com
Wed Dec 12 23:44:27 UTC 2018


Hi Maurizio,

On Wed, 12 Dec 2018 at 12:20, Maurizio Cimadamore
<maurizio.cimadamore at oracle.com> wrote:
>
> [...]
> If I remove the capture conversion described in that issue, the compiler
> then will fail to compile the above example (as expected?):

Semantically, 'List<Foo<?>>' means potentially heterogeneous, not definitely.

The language cannot determine at compile time the type of the elements
present in the list and should accept any kind of potential comparator
of these elements which may or may not fail at runtime depending on
what's present in the list.

It's interesting to see that your example fails when invoking
'Integer::compareTo' but not when invoking 'Comparator::compare':

http://hg.openjdk.java.net/jdk/jdk/file/4bb6e0871bf7/src/java.base/share/classes/java/util/Comparator.java#l469

You could even have a comparator that successfully compares
heterogeneous elements.
So, at my mind, the current behavior inherent to potentially
heterogeneous collections is sound.

Bernard


More information about the compiler-dev mailing list