Closures in Java: Too Much Typeing!
Danno Ferrin April 28th, 2007
Danno Ferrin wrote about Java 7 closures:
A consensususes has been reached on adding Closures to Java 7. While this is good for Java I’m afraid I may never use it because there is just too much typing. My fingers will get sore typing out all those class names! Either that or my spacebar will break when the IDEs catch up. Let’s do a small comparison of some of the samples from the currently proposed Java closures specification and Groovy. First: Java,
{int,int=>int} plus = {int x, int y => x+y};
45 characters and 5 types. Now for groovy:
def plus = {x, y -> x+y};
23 characters, whoops, 22 because the semi-colon is not needed. And only one type (if you count the any-type ‘def’), zero if you execute it in a script. Of course the Java version will likely run in less than one hundredth of the time *ducks*.
Commentors said:
You have two compare the number of keystrokes of it takes to define a Java closure to the number of keystrokes it takes to achieve the same goal without using the closures in Java. And yes, it is a relief!
If someone put a one dollar bill, and a five dollar bill in front of you and told you to take as much money as you wanted, what would you do? Java 6 is the one dollar bill, Java 7 would be the five dollar bill. Or you could jsut take them both and call it Groovy.
There is also an ongoing idea to give Java type inference, so then you could do:
final plus = {int x, int y => x+y};
And we’d still have refactoring IDEs.
Tags: Danno Ferrin
![[TypeKey Profile Page]](http://shemnon.com/speling/nav-commenters.gif)
About
Leave a Comment