Bruce Eckel
Posts: 875
Nickname: beckel
Registered: Jun, 2003
|
|
Re: Generics: Unbounded wildcard puzzle
|
Posted: Nov 7, 2005 2:14 PM
|
|
> "There is one special rule for unbounded wildcard > instantiations: the conversion from a raw type to the > unbounded wildcard instantiation is not an "unchecked" > conversion, that is, the conversion from Collection to > Collection<?> does not lead to an "unchecked" warning. > This is different for concrete and bounded > d instantiations, where the conversion from the raw type > to the concrete and bounded wildcard instantiation leads > to an "unchecked" warning." > > Another way to put it is that Collection<?> is the > supertype of any Collection, whether raw or parameterized, > while Collection<? extends Object> is the supertype only > of parameterized Collections. > > That doesn't explain why Sun decided to do it that way, > though.
Yes. This is slightly helpful, but basically it's only describing the way things (apparently) work, without any intuitive sense of why.
My perception is that the compiler is saying that: List<?> unbounded = new ArrayList(); while not exactly correct, is tolerable, so it won't bother issuing a warning. They are actually two different things, but it's not worth making a fuss about.
However, I suspect there may be a motivation behind that, which might be the ability to say:
void f(List<?> anyList) { /* ... */ }
means "I'll accept any type, even though I actually know what generics are."
But, in addition, List<?> is also used for capture conversion.
It may also be that there are several meanings intended for <?> and thus it comes across as being imprecise.
|
|