So I was listening to a presentation of proposed language features for Java 7 on infoq . Of course the prospective closure feature came up. There is a good article on javaworld that I have read a couple of times to try and understand the subject some more.
It does give a good basic summary of what a Closure is,
"Essentially, a closure is a block of code that can be passed as an argument to a function call, which will execute the block immediately or some time later. To some extent, a closure is similar to an unnamed (or anonymous) function. There is more to it, but for the time being, let us stick to this definition. "
So it does not seem that bad on the face of it. My headache comes when I start thinking about adding another feature to a language that is already getting syntactically very complex. Closures are not simple and if you read the article the fact that there are three competing specs at this moment shows how complex a subject it is.
The classic argument of "well you do not have to use the feature" does not hold water with me. Developers love to use new features even if they are a bad fit. I have read many times code to do some basic feature that is mind blowing in its complexity because someone wanted to use a language/framework feature they had just read about.
It does look like Java 7 may be spared Closures. Because there are three specs and lots of arguments means probably it will not make it in. Let's hope this is the case. Otherwise I am looking forward to Closures becoming some weird interview question.
Agreed. Better to let people who want modern language features to migrate
to modern languages. Java is going to be a legacy platform for some time to
come, do we really want to exacerbate things by encouraging people to write
new Java code? Code that's not even compatible with the existing legacy? We
don't need the complications.
Dont drop Java there to much investment. C++ will soon be more modern than
Java. If Java is not modern it is legacy and I will not use it for new
stuff. I want closures, I already use them in other languages in my work it
would not make my life more complex.
Java is simpler than C++. So if your argument was correct Java would't
stand a chance agains C++, especially in 1998 when Java didn't even have
Generics. Being more complex/moderd is not a good thing, it's a bad thing.
Except for the gurus of course, but thos are like 2% of the developers (but
70% of the bloggers)
"the fact that there are three competing specs at this moment shows how
complex a subject it is"
Wow obviously Closures is a hot topic. Especially if a small blog like
mine gets 4 posts on the subject.
@David
Here is how closures help you keep DRY. It's not dramatically better than
using an inner class because it doesn't really use the power of closure to
close over non-final variables. But it's easier than writing an inner
class.
List<A> filtered = new ArrayList<A>();
for(A a : allitems) {
if(a.isGood()) {
filtered.add(a);
}
}
With first-class functions, you can factor this into:
List<A> filter(Function<A,Boolean> f, List<A> l) {
List<A> filtered = new ArrayList<A>();
for(A a : l) {
if(a.isGood()) {
filtered.add(a);
}
}
return filtered;
}
/* and */
filter(isGood, allitems);
/* or */
filter(isCurrent, newsitems);
You're no longer repeating the series (1) new List (2) for (3) if (4) add.
In systems with good OO/functional integration, you should be able to use
first-class methods too, something like filter(A.isGood, allitems)
or filter(_.isGood, allitems). You can do this with inner classes,
of course, but the syntax is almost as long as the original and many times
longer than just A.isGood.
In fact, the best use case for closures in an OO language is not the twisty
confusing modification of state. If you're going to do that, use a class!
That's what they're for.
I should finish off with a disclaimer that I am not a Java programmer. I
write Python and Lisp, where passing functions around is common. But I've
recently had to write some Java for the first time in 4-5 years and despite
how cool the changes sound from the outside, the language is worse for
them, I think. Greg M is right: if you want modern features, use a modern
language. Leave Java simple like it started out.
Closures are a bit hard to grasp initially, but once you know how they
work, they are very powerful.