High Level thinking about the introduction of closures in Java
The term "closure" is defined as follows:
"In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables. The explicit use of closures is associated with functional programming and with languages such as ML and Lisp. Constructs such as objects in other languages can also be modeled with closures"
The first paragraph is a little bit too abstract, but it is getting better:
"A closure can be used to associate a function with a set of "private" variables, which persist over several invocations of the function. The scope of the variable encompasses only the closed-over function, so it cannot be accessed from other program code. However, the variable is of indefinite extent, so a value established in one invocation remains available in the next. As a consequence, closures can be used to hide state, and thus to implement object-oriented programming.
The concept of closures was developed in the 1960s and was first fully implemented as a language feature in the programming language Scheme. Since then, many languages have been designed to support closures."
Now it is getting clear - closure is something like a "crippled" method, which you can pass around. However this method is special - and has the access to the invocation scope - so you could access the state of the object, from which it is invoked. A closure is somehow similar to a inner class - with hopefully more straight forward syntax. Some observations:
- Closures are used mostly in functional languages
- The concepts of closures is about 50 years old
- Closures are especially useful for API design
Java isn't a functional language and the closure debate arised just in the recent two years. I don't think, that it would be possible to refactor big portions of e.g. the Java-libraries before Java 7. So we would have to live with the mix - legacy java.util and java.lang libraries with probably new, more concise designed Java 7 libraries. Some closure examples from Neal Gafter's blog.
I'm not sure, whether it is a really good idea to push Java in the functional direction - ono the other hand - with closures you can reduce a significant amount of code - which is always a good thing. What I observer too: many developers are getting really excited about closures - it is a really nice topic for getting warm in a meeting as well:-).
Well there are thousands (millions?) of "claimed to be" Java developers who even doesn't know how to use the current Java feature set. However but they boldly use them in several inappropriate manner. Mixing these minds with closures could easily end of maintainability of several projects.
That's why I fear from closures.
Posted by Laszlo Kishalmi on April 07, 2008 at 03:48 PM CEST #
Do we know whether Sun has started to work on closure already?
Even though closure is a really good idea, adding it would cause many changes, for example, Garbage Collection, which may end up too much to change.
Posted by Chris Lu on April 07, 2008 at 11:53 PM CEST #
There is a prototype of closures for Java you can use to learn more about them. See http://www.javac.info/
There is no need to refactor any of the existing Java libraries. Closures don't require any changes to garbage collection.
Posted by Neal Gafter on April 08, 2008 at 06:59 PM CEST #