Comparator

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Jul 15 09:53:37 PDT 2013


I ran your test and I realized that there's still one piece of work left 
to do in this area - which is to use inference constraints during the 
most specific check. The compiler is doing all the right things, but 
then most specific test is currently requiring that all lambda-shaped 
arguments are applied to functional descriptors with the same parameter 
types; here you need a bit of inference to prove that, otherwise the 
parameter types are just different.

Maurizio

On 15/07/13 17:38, Maurizio Cimadamore wrote:
> On 15/07/13 17:28, Marc Petit-Huguenin wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA256
>>
>> On 07/15/2013 09:18 AM, Maurizio Cimadamore wrote:
>>> On 15/07/13 17:15, Marc Petit-Huguenin wrote: On 07/15/2013 09:09 AM,
>>> Maurizio Cimadamore wrote:
>>>>>> On 15/07/13 16:53, Marc Petit-Huguenin wrote: Hi,
>>>>>>
>>>>>> I am trying to use a comparator on a Map.Entry<String, Integer>
>>>>>> stream, and I am not sure why this does not compile:
>>>>>>
>>>>>> .sorted(Comparator.comparing(Map.Entry::getValue))
>>>>>>> This is as expected - the method reference Map.Entry is defined on
>>>>>>> a generic class with unknown parameter types (some Map<K, V>) -
>>>>>>> therefore the compiler needs the info from the target (in a
>>>>>>> top-down way) in order to decide what the method reference looks
>>>>>>> like. But the target type info is incomplete as it depends on
>>>>>>> inference variables on its own. So the compiler ends up applying a
>>>>>>> simpler (arity-based only) check during overload resolution - which
>>>>>>> means the compiler can't really pick the right comparing() method
>>>>>>> ahead of the target-type. A shorter way to get this to compile
>>>>>>> should be this: .sorted(Comparator.comparing(Map.Entry<String,
>>>>>>> Integer>::getValue))
>>> I forgot to tell that I tried this too.  It fails with a "error: reference
>>> to comparing is ambiguous", but not with the new "inference loop" error.
>>>
>>> Note that ".sorted(Comparator.comparing((Map.Entry<String, Integer> entry)
>>> -> entry.getValue()))" (i.e. without the .intValue()) fails with a similar
>>> error.
>>>> Are you using the official JDK 8 or the experimental lambda-repo
>>>> compiler? Only the latter contains a fix for this.
>> I built it yesterday from the lambda repository:
>>
>>    .:5faccb5b660e corba:08e436473edb hotspot:7b772f9e0365 jaxp:ae467ce203a8
>> jaxws:f2e56f100ecf jdk:00b08a855e2e langtools:f75d9c517541 nashorn:dd371db92a91
> Ok - I'll dig it more and come back to you
>
> Thanks
> Maurizio
>
>
>>>> Maurizio
>>>>>>> Let me know if that works for you. Maurizio
>>>>>> error: reference to comparing is ambiguous error: incompatible
>>>>>> types: Cannot instantiate inference variables T because of an
>>>>>> inference loop
>>>>>>
>>>>>> (interestingly the second error was not present when using a build
>>>>>> from last week)
>>>>>>
>>>>>> These two compile:
>>>>>>
>>>>>> .sorted(Comparator.comparing((ToIntFunction<Map.Entry<String,
>>>>>> Integer>>)Map.Entry::getValue))
>>>>>>
>>>>>> .sorted(Comparator.comparing((Map.Entry<String, Integer> entry) ->
>>>>>> entry.getValue().intValue()))
>>>>>>
>>> -- Marc Petit-Huguenin Email: marc at petit-huguenin.org Blog:
>>> http://blog.marc.petit-huguenin.org Profile:
>>> http://www.linkedin.com/in/petithug
>>>
>> - -- 
>> Marc Petit-Huguenin
>> Email: marc at petit-huguenin.org
>> Blog: http://blog.marc.petit-huguenin.org
>> Profile: http://www.linkedin.com/in/petithug
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.4.12 (GNU/Linux)
>>
>> iQIcBAEBCAAGBQJR5CMqAAoJECnERZXWan7EQs8P/2Sh+duiYTm0hEbofYXocAWn
>> ltYRk9ClO2lMEqJ0+WbT7qxqdbahORowkUH53UlNvMq3PP94M/AodKb4ap1B2O0M
>> I1sYBc459hp8t4uw+FU/Cb1w3uy0ZR6P2g7c064y3kFXO6Caxl6mLmmwuOm68kwv
>> ZtZM8pRQkdPDw3VAVNshzZqhm3ZiMMKGY2Q8l9SbLsKy2U+wxo3wluFP6jl+cXr5
>> mJ8wx07fT4uJ/xmiO2pdCMONWOW7UC6tIt4NGGgOjt6K04rSsUeG21mjAfSS2cuW
>> CLn/7BKBBJp8S7hRf8ieVsiYLYtTOYL2SxwiEG9pFa/Ftk4AxyBZlIjnE9+vOF3D
>> 4CxUmEW/hRQZeoqcQ19Pi4AKl1OMbRE4O/yDxFmqgc/7bQEhItHtOfsFQbFWT8P1
>> fDvigZiSUzu+cTu9U+QlRlQx7xkR4sOkb91qflIxs/W5EDMKtyCT0wgk4Ew0xXch
>> 7ZXT/phohxUZTu40vB8TCIVq+PQjMSX6g79z1b5EXoxDH7yktxQ43HuLaQa5OZD/
>> 83x4bMhqlIWMEP248pQIoBf93E3dHyW5k1080lsbT5jro9/mHsncf6E4hnA2SBWO
>> /Xy6hzV7aLtGRQlvoWATTWAaQjXHroW9h1MfxBTWxE5sm+uL10chjaF/gEbC8Blz
>> vrEGFEpJIDhnxdAEl717
>> =iQ8u
>> -----END PGP SIGNATURE-----
>



More information about the lambda-dev mailing list