intersection type compatibility

Stephan Herrmann stephan.herrmann at berlin.de
Tue Dec 3 07:26:07 PST 2013


Consider this program:

   import java.util.ArrayList;
   import java.util.List;

   public class Compile {

       public <T, Exp extends List<T>> Exp typedNull() {
           return null;
       }

       public void call() {
           ArrayList<String> list = typedNull();
       }
   }

May I safely assume that the following error issued by
javac b117 is a bug, or am I missing a subtle issue
about intersection types?

   Compile.java:11: error: incompatible types: inferred type does not conform to upper bound(s)
           ArrayList<String> list = typedNull();
                                             ^
       inferred: INT#1
       upper bound(s): List<Object>,ArrayList<String>
     where INT#1 is an intersection type:
       INT#1 extends ArrayList<String>,List<Object>
   1 error

BTW, javac 7 gives a less puzzling error, but the
new inference seems to obsolete this error:

   Compile.java:11: error: invalid inferred types for T,Exp; inferred type does not conform to declared bound(s)
           ArrayList<String> list = typedNull();
                                             ^
       inferred: ArrayList<String>
       bound(s): List<Object>
     where T,Exp are type-variables:
       T extends Object declared in method <T,Exp>typedNull()
       Exp extends List<T> declared in method <T,Exp>typedNull()
   1 error


thanks,
Stephan



More information about the lambda-spec-observers mailing list