N
Neal Gafter
Kirk said:Thanks for the response, Neal, but I'm aware it's possible to
literally pass the Class type that you are trying to check for and use
its isInstance method. But the point I was trying to make is
intuitively, this information should already be present because of the
generic type (and would be present without type erasure) specified on
the class.
The example you sited is a static method that wraps an already typed
collection (type E) but still requires you to supply the class type.
Thus, for a collection of strings, you would have:
Collections.checkedCollection(new ArrayList<String>(), String.class);
What I find unannoying and unintuitive is the need to supply
"String.class" when the type of the collection is already supplied via
"new ArrayList<String>()". I guess ultimately this is just an
inconvenience, but seeing all this redundant code really clashes with
the whole spirit of generics.
I agree completely. That is the price we pay for transition compatibility. See
section 6 of <http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf>. If we
were designing the language from scratch and didn't have to worry about
interoperating with existing code we certainly would prefer to reify type
parameters.