Friday, December 28, 2007

Java Closures Tutorial

Java Closures Tutorial

JSR Proposal: Closures for Java, section 2:

'This JSR provides support for operating on an arbitrary "block of Java code", or body, which is either a statement list, an expression, or a combination of both. We call the mechanism a closure expression. Wrapping statements or an expression in a closure expression does not change their meaning, but merely defers their execution. Evaluating a closure expression produces a closure object. The closure object can later be invoked, which results in execution of the body, yielding the value of the expression (if one was present) to the invoker. A closure expression can have parameters, which act as variables whose scope is the body. In this case the invoker of the closure object must provide compatible arguments, which become the values for the parameters.'

This tutorial does not describe the whole proposal but only the features implemented in the prototype compiler. All examples in this tutorial were tested against the early prototype available at http://www.javac.info. Current version: closures.tar.gz

7 comments:

danrevel said...

How do I build and test against the closure prototype code?

Hamlet D'Arcy said...

As long as you have Java 6 in your path (you'll need to execute java and javac from the command line), then you can unzip the files and move to the bin folder within there, and then call java.bat and javac.bat to use the prototype.

It would be great if the .ztar file could contain an example, but I got it all working so it's not a big issue for me.

Anonymous said...

Java boys, time to learn Lisp!

Anonymous User said...

As far as the Closure Syntax have you thought of alternatives to "=>" ?

I've always like how "::" looks if nothing else, and C++ confusions aside, if I saw "::" in a Java program I wonder what it was doing there. "=>" would surely be read as a boolean operation by the uninformed.

Zdeněk Troníček said...

There are several alternatives to "=>". My personal preference is "->" but "::" is not bad too.

beltorak said...

I have worked for the last day over an alternate syntax - is it too late to fundamentally change the syntax? My intent is to provide a syntax that is more or less consistent with the rest of the Java language, making it more easily intuitable; I abhor the '=>' - mainly because it is hard for me to type that sequence in rapid succession, but also because it makes Java feel less... Java-y. I like using '#' as a closure signifier (much like '@' is used as the annotation signifier) as in the method references, but with closure declarations and definitions bounded by '#{' and '}'.

My text file of examples and comments is about 700 lines so I posted it on pastebin. It may not be entirely internally consistent as some of the ideas I was working out as I wrote it.

Please let me know, even if it's a "sorry, we've settled on the arrows".

Thanks

Zdeněk Troníček said...

Hi beltorak,

as far as I know, it is not late because the closures proposal have not been approved yet.