The Cafes » Type Inference: Another Bad Idea for Java 7 (original) (raw)

Peter von der Ahé and a few others are pushing type inference in Java 7. The goal is to not have to explicitly declare local variable types. Remi Forax offers this example:

// print a frequency table of words public static void main(String[] args) { map := new HashMap<String, Integer>(); for(word : args) { freq := map.get(word); map.put(word, (freq==null) ? 1 : freq+1); } System.out.println(map); }

Peter von der Ahé and Christian Plesner Hansen suggest reusing the final keyword instead:

public static void main(String[] args) { final map = new HashMap<String, Integer>(); for(final word : args) { final freq=map.get(word); map.put(word, (freq==null) ? 1 : freq+1); } System.out.println(map); }

Note that both proposals have the side effect of making the local variable final, as well as inferring its type, although it’s more explicit in the Ahé proposal.

Type inference actually makes some sense in languages like JavaScript and PHP that are built around this, and had this feature from day 1. It makes no sense in a language like Java that’s built around the opposite. It makes Java look weakly typed, but it isn’t. In fact, if anything this is now more strongly typed because, for example, you have to type a Map variable as a HashMap or a TreeMap rather than just a Map. That is,

map := new HashMap();

is equivalent to

HashMap map = new HashMap();

not

Map m = new HashMap();

Much more importantly, though, it’s more syntax that makes the language larger and harder to learn. I used to be able to teach pretty much all the important parts of Java in one semester. Now I can’t even think about doing generics, annotations, and enums unless I drop GUIs. Java 1.0 was designed as a good teaching language. Java 1.1 was pretty good too. Every piece added to it since then—anonymous inner classes, strictfp, closures, enhanced for loop, generics, type inference, etc.—made it less suitable for that purpose.

Even if I try to teach type inference, I have to cover a lot of special cases, and explain what it means to make a local variable final first, and why you might want to do that. This makes some sense to those of us who’ve been breathing this stuff for 10+ years now. It makes no sense at all to anyone who’s coming to the language for the first time. It’s just one more source of weird, incomprehensible compiler error messages they have to ask their professor to debug for them. Is this really worth saving a few keystrokes of typing?

It’s time to call a halt to new features in the Java language. I am not saying that the features are wrong, just that they don’t fit into the language any more. It’s already too bloated. I am not saying that generics, type inference, closures, compiler created factory methods, and other kitchen sink proposals are bad. They’re not. I am saying that they simply don’t fit into or with the current core language, and every one we add makes the language worse, not better.

If these features are necessary, and some of them may be, then they should be designed into a new language from the beginning. This new language could have real generics without type erasure. It could have := but not =. It could have factory methods and not have constructors. It could have closures and not have anonymous inner classes. That would be a language that made sense, and that you could still teach in one semester without tripping over 37 different special cases.

However the current push to add new language features to Java without taking anything away must stop. Every single one makes the language worse, not better. Every single one makes Ruby and C# and Python look more attractive. You cannot build on top of an old foundation forever. Fifty story skyscrapers cannot be built by adding floors to a five-story tenement. Sometimes you have to move down the block and start building in a new lot. For Java, that time has come.

This entry was posted on Monday, April 16th, 2007 at 7:13 am and is filed under Blogroll. You can follow any responses to this entry through the Atom feed. Both comments and pings are currently closed.