Community
Participate
Working Groups
(Observed against 3.1 M4:) The compiler reports this warning: --- Varargs argument Collection<? extends E>[] should be cast to Collection[] when passed to the constructor UnionIterator<E>(Collection...) --- where the constructor in question of UnionIterator<E> is declared as public UnionIterator(Collection<? extends E>...) { [...] } and I invoke it as Collection<? extends T>[] collections = [...]; UnionIterator<T> iterator = new UnionIterator<T>(collections); There are several problems with this: (1) The compiler is losing the type information for the varargs argument. (Although it seems to remember the type information until the end of the file in which it appears, because the warning only showed up when I moved the UnionIterator class to its own file.) (2) Collection is not a supertype of Collection<? extends E>, thus the warning message is demanding an unsafe cast. The compiler should instead be issuing a type safety warning if it cannot verify the type-correctness of the constructor invocation (though better would be for it to recognize that the invocation is indeed type-correct). (3) If for some reason the compiler *cannot* retain the type information in such a case, then it should flag the constructor declaration with a warning.
Cannot reproduce in 3.1. This code is compiled without any warning: public class UnionIterator<E> { public UnionIterator(Collection<? extends E>... arg) { } void test(Collection<? extends E>[] collections) { UnionIterator<E> iterator = new UnionIterator<E>(collections); } } class UnionIterTest { static <T> void test(Collection<? extends T>[] collections) { UnionIterator<T> iterator = new UnionIterator<T>(collections); } }
I agree that the warning observed under 3.1 M4 is not observed under 3.1.0. Evidently this issue has been resolved. Thanks for looking into it.
closing