Wednesday, August 04, 2004

More on generics

Unfortunately, Sun's generics are not as similar to C++ templates as one might hope. For example, because of type erasure there is no difference between Foo<Bar> and Foo<Baz> in bytecode. That means that you cannot compile this appealing snippet:

class Peter implements Receiver<PaulMessage>, Receiver<MaryMessage> { // ... public void onEvent(final PaulMessage paulsCalling) { } public void onEvent(final MaryMessage marysKnocking) { } }

Why not? Because Receiver<PaulMessage> and Receiver<MaryMessage> both compile down to just Receiver JDK5 does not permit duplicate declarations in the implements list. This greatly lessens the usefulness of my previous post.

No comments: