Skip to content

Horrible Java Generics syntax

by Priyatam

Class Formatter

public interface Formatter <t extends Foo> {
    public List<list<string>> format(List<t> foos);
}

Class FooFormatter implements Formatter and is strongly typed

public class FooFormatter<foo> implements Formatter {
    public List<list<string>> format(List<foo> beans) {
        return null;
    }
}

When you compile the above (or see the red marks in your IDE), you get the following compiler error:-

FooFormatter is not abstract and does not override abstract method format(java.util.List) in Formatter

What is so wrong with that? Here’s the correct version:-

public class FooFormatter implements Formatter<foo> {
    public List<list<string>> format(List<foo> beans) {
        return null;
    }
}

Fair enough, I get it, but seriously — what does the previous compiler error mean?

“Strongly typed classes,” can be good at times, but the syntax of Java Generics is so complicated, it makes you wonder about the implementation of the same. Just think of instantiating a HashMap containing a set of Generic classes, which itself contains another Generic classes. The declaration and initialization is so long, it might well cross the 150 meter boundary in your IDE.

Here are some rants on why Java Generics sucks. One more here and a very useful Generic FAQS here

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*