extend and type inference
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Tue Aug 7 07:33:49 PDT 2012
On 07/08/12 08:01, Jose wrote:
> Build b50
>
> In this simple case the compiler can't infer the type of the variable r:
>
> public static void main(String[] args) {
> Iterable<? extends Rectangle2D> it=new ArrayList<>();
> Iterable<Integer> originPos=it.map(r->r.outcode(0,0));
> }
>
> You must to set the type by hand:
>
> public static void main(String[] args) {
> Iterable<? extends Rectangle2D> it=new ArrayList<>();
> Iterable<Integer> originPos=it.map((Rectangle2D r)->r.outcode(0,0));
> }
>
>
The problem is that the type inferred by the compiler for 'r' is not the
optimal one - the inferred type is a captured type #1, where #1 extends
Rectangle2D - while most users would expect the inferred type to be just
Rectangle2D. The problem the compiler is running into is that, if #1 is
the inferred type, the compiler cannot show that
Mapper<#1, ...> is a subtype of Mapper<? super #1, ...>
we are looking into this to see if there something we can do.
Maurizio
More information about the lambda-dev
mailing list