Monday, April 30, 2007

Consensus Reached on Java Closures Proposal

A Consensus about Java Closures has been reached and you can see the JSR proposal here.

"After months of debates, Neal Gafter, co-author of one of the proposals announced on his blog that a consensus was reached, and that the authors of the three proposals all agreed to stand behind one JSR request to add closures to the Java language"

from http://www.artima.com/forums/flat.jsp?forum=276&thread=204090

As I already said on this blog, Closures are blocks of code easily manipulated, e.g, attributing it to variables, and understandable as well. They are like a simplification of inner class, but they're simpler. Below you can see an anonymous inner class:

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(e.getSource());
}
});


As you can see, it's a bit complex. Using closures you'll have something like this:

button.addActionPerformed({e => System.out.println(e.getSource())})


More simple, more clean. But the possibilities don't stop here, you'll be able to attribute closures to variables and invoke it too, see:

{int,int=>int} plus = {int x, int y => x+y};
plus.invoke(2, 3);

I got this example from Closures for the Java Programming. There you can see more details about Closures in the Java Programming. Enjoy it.

No comments: