Community
Participate
Working Groups
This code compiles with eclipse 3.2 & 3.3M1 without error (there is the expected warning about the variable set not be used). However it fails to compile with the sun compiler version 1.5.06. When I compile the code with the sun compiler I get the following errors: [checking GenericsTest] GenericsTest.java:12: incompatible types found : java.util.Set<GenericsTest.B> required: java.util.Set<GenericsTest.A> Set<A> set = g.newSet(g.new B()); I'm not sure who is correct here. The generics syntax looks correct to me. import java.util.LinkedHashSet; import java.util.Set; public class GenericsTest { public class A {}; public class B extends A {}; public static void main(String[] args) { GenericsTest g = new GenericsTest(); Set<A> set = g.newSet(g.new B()); } public <T, V extends T> Set<T> newSet(V... objects) { Set<T> set = new LinkedHashSet<T>(); for (T t : objects) { set.add(t); } return set; }
I believe the Eclipse compiler is right, and javac wrong. According to JLS 15.12.2.8, the return type Set<A> should be inferred from to bind the unconstrained variable T.
Interestingly enough, removing the bound on parameter V makes javac happy again. i.e. public class GenericsTest { public class A {}; public class B extends A {}; public static void main(String[] args) { GenericsTest g = new GenericsTest(); Set<A> set = g.newSet(g.new B()); } public <T, V> Set<T> newSet(V... objects) { Set<T> set = new LinkedHashSet<T>(); //for (T t : objects) { // set.add(t); //} return set; }
Added GenericTypeTest#test1026-1028
Marking as invalid (we do the right thing) Thanks for the testcase
closing
also see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6468384