heads-up: biggie overload rewrite

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Fri Jul 26 04:01:21 PDT 2013


On 26/07/13 00:05, Ali Ebrahimi wrote:
> Hi,
> But, This one fails,
> Comparator.comparing(String::length);
>
> java: reference to comparing is ambiguous
>   both method <T>comparing(java.util.function.ToLongFunction<? super 
> T>) in java.util.Comparator and method 
> <T>comparing(java.util.function.ToDoubleFunction<? super T>) in 
> java.util.Comparator match
Yep - this is what I'm about to fix - note that String.length is non 
overloaded, so compiler can go ahead and use that information to decide.

Maurizio
>
>
> On Fri, Jul 26, 2013 at 3:25 AM, Ali Ebrahimi 
> <ali.ebrahimi1781 at gmail.com <mailto:ali.ebrahimi1781 at gmail.com>> wrote:
>
>     Hi,
>     This works for me.
>
>     Comparator.comparing((String s) -> s.length());
>
>
>     On Fri, Jul 26, 2013 at 2:56 AM, Maurizio Cimadamore
>     <maurizio.cimadamore at oracle.com
>     <mailto:maurizio.cimadamore at oracle.com>> wrote:
>
>         On 25/07/13 23:13, Richard Warburton wrote:
>         > Hi,
>         >
>         > >From a language perspective, the problem is simple: we
>         can't type check
>         >> 's.length()' until we know that 's' is a String, and we
>         can't figure that
>         >> out until we look at the assignment target, and we can't do
>         that until
>         >> overload resolution is done.  In other words, there is not a
>         >> context-independent way to figure out that
>         >> 'Comparator.comparing(s->s.length())' is best treated as an
>         invocation of
>         >>   'comparing(ToIntFunction)'.
>         >>
>         > Correct me if I'm wrong, but at least this means that
>         developers should be
>         > able to provide sufficient information for the compiler to
>         correctly infer
>         > the lambda expression's type by writing:
>         >
>         > Comparator.comparing((String s) -> s.length())
>         Correct - that's indeed the preferred approach. Note that this
>         doesn't
>         work merely because of a bug, which I will fix soon.
>
>         Maurizio
>         >
>         > rather than
>         >
>         > Comparator.comparing((ToIntFunction<String>)(s -> s.length()))
>         >
>         > I suspect that providing argument types is a lot less of a
>         leap for a
>         > developer to make when fixing a compile error than providing
>         a full
>         > functional interface type.
>         >
>         > regards,
>         >
>         >    Dr. Richard Warburton
>         >
>         > http://insightfullogic.com
>         >    @RichardWarburto <http://twitter.com/richardwarburto>
>         >
>
>
>
>



More information about the lambda-dev mailing list