Community
Participate
Working Groups
I20060815-0800 Arrays.asList(1, "X"); The warning is: "Type safety : A generic array of Object&Comparable<?>&Serializable is created for a varargs parameter" - I don't see a type safety problem, since the only "genericity" in the array type is Comparable<?>, but 'new Comparable<?>[] {1, "X"}' also doesn't trigger a warning. - There's a space too much between "Type safety" and ":" - "Object&Comparable<?>&Serializable" would be easier to read if there were spaces around the "&". The innovative rendering of the warning in the status bar is bug 78746.
Re: spaces around '&' in intersection type It may confuse readers since they wouldn't see an intersection type as one piece, but as multiple fragments... Re: type safety issue This behavior is described in the spec I think. Need to double check on this.
Tim, pls have a look at it.
Javac doesn't report the warning, but technically I think it should. An intersection type isn't reifiable, though it seems to treat it as such. Comparable<?>[] is tolerated by the spec as an unbound parameterized type (but this is an exception).
As per JLS4.7, an intersection type isn't reifiable, and should thus be warned against for varargs invocations. Added GenericTypeTest#test1136
Closing as invalid.
Never mind, if I ever need to avoid the unchecked warning, I can always write it like this: Arrays.<Comparable<?>>asList(1, "X") // no unchecked warning in ecj or javac However, it's interesting that both ecj and javac protest against Arrays.asList(new ThreadLocal<Runnable>(), new ThreadLocal<Cloneable>()); but accept Arrays.<ThreadLocal<?>>asList( new ThreadLocal<Runnable>(), new ThreadLocal<Cloneable>()); (probably because inference for the former yields ThreadLocal<? extends Object>).
Exactly. I agree this doesn't feel intuitive (blame the spec <g>).