|
Re: Using Wildcards in Java Generics
|
Posted: Jul 3, 2008 2:33 PM
|
|
Thinking about it more over a late lunch.
In general, I don't write into Collections passed in from the outside. I create my own. Call it "functional-like", or fear/confusion whether it is o.k. to write into their collection, something very few programmers document very well in my experience. When I do write into other Collections, it's a well defined concrete type like String, or a completely ill-defined type like Object. So I never/seldom need "super".
In general, our method signatures are Collection<FairlyAbstractType>, not Collection<SpecificType>. If everybody has been consistent with FairlyAbstractType and your code is well factored/designed (or you are just plain lucky!), we don't need extends. Probably should use it more, but don't. The knowledge that the collection is really of <SpecificType> is "in the comments" or "understood", and once in a while we do the unsafe casts. While James is right that in an ideal world it would be best to bring this knowledge to the compiler, I confess to James and agree with Alan that after a point it's not worth it.
p.s.
Re-reading this post, seems to me that perhaps "? extends" should have been "readable-as" and "? super" sould have been "writable-as". And, "readable-as" should have been the default cause that's what I use the most. :-)
|
|