Status of Java Closures
Since before Java SE 6 came out, a discussion has been going on in the Java community future directions and extensions of the Java language, with the most energy directed on closures. There are currently two major camps on this issue, the BGGA proposal and the CICE proposal.
CICE
Concise Instance Creation Expressions by Bob Lee, Doug Lea, and Josh Bloch favor adding simple syntactic sugar for anonymous inner class creation, where
Listcan be written asls = ... ;
Collections.sort(ls, new Comparator() {
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
});
Listls = ... ;
Collections.sort(ls,
Comparator(String s1, String s2){ return s1.length() - s2.length(); });
Additionally, it allows capture of and access to the local variables of the enclosing block(s). In general it's a small proposal, with few warts, that simply provides for small gains in legibility.
BGGA
BGGA is the proposal by Gilad Bracha, Neal Gafter, James Gosling and Peter von der Ahé, which is much more heavyweight, providing for true closures, function types and user-defined control abstractions. The above example could be written as
Listls = ... ;
Collections.sort(ls, {String s1, String s2 => int s1.length() - s2.length()});
However, this proposal leads to some weird interactions with expected behaviour:
...The previous snippet will not print "2", as the return is interpreted as a return from the defining method, not the closure; a new type has been added (java.lang.Unreachable) so that the previous code should not compile. What happens tho if you store the closure and invoke it after the defining method has returned? See the Joshua Bloch presentation at JavaPolis 2007 for a recap of the present problems.
System.out.println("1");
{=>return;}.invoke();
System.out.println("2");
...
All in all, while BGGA adds new powerful concepts and new problems to the Java language, the community is still undecided on whether the inherent problems with closures are justified by the expressiveness they enable; several voices advocate leaving Java unmodified and switching over to JVM-based languages that have them built in, such as Scala.
Further Reading
- Closures Sources
- Neil Gafter's blog
- Automatic Resource Management Blocks, automatically closing resources on block exit.
- Expanders: Statically Scoped Object Adaptation for Java



Comments