November 20
Bringing closures closer to Java
Posted by AjaySome bits of news (good?) that has been trickling through the blogosphere is that Java is finally gonna have Closures (whooohooo). Mark Reinhold announced it was time for closures. This would probably be one giant step for Java. Having observed closures in action in Javascript and Groovy, this can definitely affect the way we see Java.
What are closures?
In simple terms closure is dynamic code that can be passed around as objects and as argument to a method(quite similar to pointers to functions in C, but with a difference). In fact closures can refer to variables defined outside the closure within the scope of the closure definition. Function pointers cannot do this.
Here is a simple example from Groovy
1 2 3 | def clos = { println "hello!" } println "Executing the Closure:" clos() |
A major distinction between closures and inner classes is that inside a closure you cannot call any other method including itself, a closure cannot call other methods defined in the closure.
Java and Closures
The closest Java comes to providing closure support right now is through anonymous inner classes - it has worked fine so far, but it’s just another name for clunkiness. JDK 7 will now include closures in some form. There are many different proposals out in the wild:
- Gilad Bracha, Neal Gafter, James Gosling and Peter von der Ahe want to build on Ruby and use Ruby like closure mechanism . They introduce function types, where each type definition defines a return type, list of argument types and set of thrown exceptions. This is also popularly known as the BGGA proposal.
Ex:1 2 3 4 5 6
public void init(JButton button) { button.addActionListener({ActionEvent ev => handleButtonPress(ev); }); }
- CICE creates a simplified syntax to define Inner classes.
Ex:1 2 3 4 5 6
public void init(JButton button) { button.addActionListener(ActionListener(ActionEvent ev) { handleButtonPress(ev); }); }
- First Class Method (http://www.jroller.com/scolebourne/entry/first_class_methods_java_style) - This focusses on methods, taking them and using them as objects. This proposal introduces a type safe way to refer to methods(maybe using #), a way to define parameters and method results, a way to create invocable method references and inner methods.
Ex:1 2 3 4 5 6
public void init(JButton button) { button.addActionListener(#(ActionEvent ev) { handleButtonPress(ev); }); }
Latest Announcements:
So it looks like the starting point of the closure functionality will be a lot of ideas from the First Class Method semantics.
JDK7 Closures are not gonna have control invocation statements nor non local returns and access to non final variables are also unlikely.
My Views on this
The syntax for (1) reminds me of groovy and I do not like it that much, especially being from a Java background using -> gives me the shivers (it reminds me of C++). FCM and BGGA look like they are completely gettign rid of inner classes. Is this a good idea, I am not sure. I like the this reference in FCM and BCGA. Both of these use this to refer to the nearest surrounding class.
But in general the proposals do not vary much except for the semantics of it’s syntax, but hey as long as I get closures I am happy.
References