13

I can see some of the benefits of closures, such as how they can have their place in simplifying existing libraries and making some future design easier and more efficient.

However, one of the key points mentioned in the draft proposal (http://www.javac.info/consensus-closures-jsr.html) is in section 2.5, point e:

(The specification will improve the language by)

e) enabling future API design to replace language design for extending the Java platform.

I'm struggling to see how this is the case, surely language design is just that - the design of the language itself, and can't be replaced by an API unless Java opens up all sorts of weird APIs using closures for modifying the language (which I highly doubt will happen.)

Can anyone shed some light on this and perhaps provide an example of something that required a language change previously but, with the addition of closures, no longer requires one?

2 Answers 2

6

API design and language features are definitely interchangeable at some points. Just look at something like the synchronized key word in Java. It's a keyword, but could just as well be implemented as an API if the language was sufficiently non-verbose. Annotations are another example. The other way around an @Stateless annotation that makes all methods in a class transactional, could also have been a language keyword.

Closures in particular make it easy to hand a "block of code" to a method, which could then do something with that.

A crude example, a for each could be made:

for_each(myFooList, #(Foo foo) { 
   String something = foo.getBar() + foo.getKaz();
   System.out.println(something);
});

Maybe not 100% as clean as having the for each loop directly supported by the syntax of the language, but it allows everyone to easily experience with language-like enhancements.

1
  • He's also looking at one of the old closure proposals for java, where one of the goals was to have syntax that was even more like Ruby (i.e. even less boilerplate in your for_each loop) than what they're discussing now.
    – Ken Bloom
    Jan 23, 2011 at 17:18
6

For anyone who hasn't read the draft proposal, here is a little more detail from later in the same document:

The addition of closures simplifies the evolution of the Java platform. Many existing language RFEs in Sun's public bug database can retargeted as API requests for methods that receive closures. Many future needs for additional statement forms can instead be met by the addition of library methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.