Generics in Java Are Evil?
Dean Wette's talk about Collections and Generics in J2SE 1.5 at the St. Louis Java Users Group tonight is livelier than usual.
Here's the gist of the talk:
- Generics are evil: The deeper I dig into the specification, the scarier it gets.
- Generics solves a problem that's not there: The kinds of errors that a type safe collection prevents are the trivial programmer errors that's easily detected with unit tests. IDE's can help with the drudgery of casting when working with Collections.
- JDK 1.5 generics uses erasure: None of the parameter type information is present in bytecode. Therefore instanceof doesn't work with generic types. Many intuitive programming constructs are illegal. The erasure decision was made so that Sun doesn't have to modify the JVM. They ended up modifying the JVM anyway.
- The new syntax is too complicated: I've been doing Java since 1996 and am pretty good at it. I'm having trouble with the syntax. Just imagine what a new Java programmer will feel.
- I would rather use C++ templates!
Dean did talk about several things that he likes: autoboxing, and the enhanced for loop. The java.util.concurrent classes were not covered tonight but were favorably mentioned.
Jeff Grigg urged programmers to take a look at C# for generics done right.
How would you like code like this?
public <T extends Comparable & Serializable> T foo(Collection<T> c1,
Collection<? extends T> c2) {
// ...
}