The debate on Closures in Java 7 has been limited to two proposals so far. Today Stefan Schulz and myself would like to offer a third option - First-Class Methods: Java-style closures (FCM) - to the Java community.
First-Class Methods: Java-style closures
Our aim when writing this proposal was to provide developers with much of the power of full-blown closures, but without the complexity. The syntax had to fit neatly within Java, but more importantly so did the semantics. The result of our work is a closure proposal that focuses on methods rather than closures, hence the name 'First-class methods'.
For those currently programming in Java, the method is the principal construct where application logic resides. Our proposal simply takes methods and allows them to be used as first-class objects within the application. But the new constructs are still recognisably methods. This results in a rapid learning curve - since all developers are already familiar with methods.
The proposal adds four new syntax elements:
- Method literals - a type-safe way to refer to a method
- Method types - a way to define the parameters and result of a method
- Invocable method references - a reference to a method-object combination that together can be invoked
- Inner methods - a way to write a method inline within another method, similar to inner classes
Full details of the syntax and semantics can be found in the First-Class Methods proposal.
Example
To whet your appetite, here is an example of creating a comparator using an inner class in Java 5:
List<String> list = ... Collections.sort(list, new Comparator<String>() { public int compare(String str1, String str2) { return str1.length() - str2.length(); } });
And here is the code rewritten using an inner method from the proposal:
List<String> list = ... Collections.sort(list, #(String str1, String str2) { return str1.length() - str2.length(); });
The inner method version will compile to almost exactly the same bytecode as the inner class version. But the syntax is much shorter and the semantics clearer.
Relationship to other proposals
There are two other closure proposals being debated. The CICE proposal aims to simplify inner class creation, while the BGGA proposal aims to introduce full-blown closures and API driven control flow statements.
The First-Class Methods (FCM) proposal is located between CICE and BGGA. FCM is considerably more powerful than CICE, which is still lumbered with the dreaded OuterClass.this syntax of the inner class. Equally, FCM doesn't get caught up in the obsessive BGGA requirement of Tennent's Correspondence Principles with its myriad of nasty implications (such as last-line-no-semicolon-return, exceptions for control-flow and RestrictedFunction). Instead FCM focuses on empowering the humble, but familiar, method.
Summary
The First-Class Methods proposal (FCM) proposal provides an alternative to the two existing closure proposals. It focusses on the familiar concept of methods, providing all the syntax and semantics necessary to use them to their full potential. The result is a proposal that gives the developer power without complexity or surprise and would make a great Java language change.
As always, Stefan and I would love to hear your feedback on this proposal.