incompatible types compile error
Arul Dhesiaseelan
aruld at acm.org
Tue Nov 20 00:43:22 PST 2012
It looks like the inference rules behave differently for these two cases.
Case 1 is illegal as per the current rules (erasure?), as you pointed out
sorted(comparing((Function<Album, Comparable>) album -> album.name)) does
return Stream<Album>, so I wonder why the compiler can't infer the type for
the terminal op into. I believe this should be legal as well?
Case 2 is legal as the type is known before the terminal op and compiler
infers just fine.
-Arul
On Mon, Nov 19, 2012 at 8:34 PM, Henry Jen <henry.jen at oracle.com> wrote:
>
> On Nov 19, 2012, at 10:10 PM, Ricky Clarkson <ricky.clarkson at gmail.com>
> wrote:
>
> > From the original post:
> >
> > .sorted(comparing((Mapper<Comparable, Album>) album ->
> >
> > Note the raw Comparable.
> >
>
> Yes, but I assume that was at the point where Mapper have the type
> declaration of Mapper<To, From>, in that case, Comparators.comparing should
> still return Comparator<Album>. I must miss some point.
>
> Cheers,
> Henry
>
>
> >
> > On Tue, Nov 20, 2012 at 2:55 AM, Henry Jen <henry.jen at oracle.com> wrote:
> > On 11/19/2012 05:37 AM, Maurizio Cimadamore wrote:
> > > The problem is that, since you are using Comparable (note the absence
> of
> > > type-parameters) you are effectively triggering an uunchecked
> conversion
> > > there - the result would be that the erased signature of 'into' will be
> > > used instead - that is, the return type of into will be simply
> > > Destination - not the type inferred from the argument - hence the
> > > incompatible types error.
> > >
> >
> > I don't quite understand this Comparable cause erased signature for into.
> >
> > Comparators.comparing<Function<Album, ? extends Comparable>> should
> > return a Comparator<Album> given correct Function type, then in
> > sorted(Comparator<Album>) should gave a Stream<Album>, isn't the type
> > obvious?
> >
> > Apology if this is a dumb question, I don't know how exactly type
> > inference works. But seems to me, the target type is available. What am
> > I missing? Thanks in advance.
> >
> > Cheers,
> > Henry
> >
> >
> > > Btw - I'm getting the error for both versions of the example you sent.
> > >
> > > Maurizio
> > >
> > > On 16/11/12 23:01, Arul Dhesiaseelan wrote:
> > >> This works, btw.
> > >>
> > >> List<Album> sortedFavs = new ArrayList<Album>();
> > >> albums.stream()
> > >> .filter(a -> a.tracks.stream().anyMatch(t -> (t.rating
> >= 4)))
> > >> .sorted(comparing((Mapper<Comparable, Album>) album ->
> > >> album.name))
> > >> .into(sortedFavs);
> > >>
> > >>
> > >>
> > >> On Fri, Nov 16, 2012 at 12:38 PM, Remi Forax <forax at univ-mlv.fr>
> wrote:
> > >>
> > >>> On 11/16/2012 10:57 PM, Arul Dhesiaseelan wrote:
> > >>>> Hi,
> > >>>>
> > >>>> I am trying to run a slightly modified version from the latest
> State of
> > >>> the
> > >>>> Lambda libraries edition.
> > >>>>
> > >>>> List<Album> sortedFavs =
> > >>>> albums.stream()
> > >>>> .filter(a -> a.tracks.stream().anyMatch(t ->
> (t.rating >=
> > >>> 4)))
> > >>>> .sorted(comparing((Mapper<Comparable, Album>) album ->
> > >>>> album.name))
> > >>>> .into(new ArrayList<Album>());
> > >>>>
> > >>>> java: incompatible types: java.util.stream.Stream.Destination
> cannot be
> > >>>> converted to java.util.List<Album>
> > >>>>
> > >>>> Any idea what could be wrong here?
> > >>> the signature of the method into.
> > >>>
> > >>>> -Arul
> > >>>>
> > >>> Rémi
> > >>>
> > >>>
> > >>>
> > >
> > >
> >
> >
> >
>
>
>
More information about the lambda-dev
mailing list