You know, I actually forget whether or not that’s planned to be allowed…people that want legit closures obviously want that (it’s pretty much what “closure” means), but if I recall there was a lot of back and forth as to whether it would be done or not, and it may have been left out. I remember some discussion about “auto-finalizing” variables whenever possible so that they could be referred to within a lambda, which implies that they came down against it, but I can’t find a good reference right now…
Personally, I don’t feel like it’s that horrible, since if you’re messing around with locals inside a closure, you’re asking for what you get - typically you would never do this unless you actually wanted to pull some state within the thing, and sometimes that’s useful to do.
I mean, is there really any difference between passing a closure that closes over a local variable to a multi-threaded operation and passing a single object reference to a bunch of different threads? Closures are often used for exactly the same kind of encapsulation that we usually do with classes in OO-land, so…
YMMV, of course.
In my experience, it’s far less bug prone than you might think, because there really aren’t that many gotchas - the one Riven noted can bite you once in a while, but if you’re futzing with multithreaded code you’re screwed anyways. The only other trouble area is control flow funny business, like continue, break, return, and exception handling; getting all that right comes down to writing a good spec, and is one of the main reasons that this has been delayed. They’re taking that very seriously, so I’d expect the end result to be pretty clear and easy to digest.
FWIW, I just checked some of my code, and in the project I’m hacking on at the moment (a wrapper around some numerical optimization code that uses reflection to let you just point at an object and say “Optimize the fields in this object relative to this utility function”, and presto!, the optimal values appear, like magic - I’m planning to lean on optimization and genetic algorithms very hard for some upcoming work, so it’s crucial to have a very simple syntax for both of them, and you can’t get much simpler than optimize(myObject, “fitnessFunctionName”), at least without first class functions. I’d estimate that this would have taken about a quarter of the time if closures were available…), almost every method has at least one anonymous inner class, and some of them go several levels deep. It’s when you start to need functions that return functions that return functions that the syntax restrictions of Java get truly obnoxious.
Perhaps that’s peculiar to my style of coding, or to my areas of interest, but I can tell you this, at least: any bugs that I’m running into are not coming from areas where I would be heavily using closures and am currently using anonymous inner classes, they’re coming from places where the logic is actually difficult. My experiences in Scala have been similar, closures are almost always a way to make your code easier to manage, even if some of the other features can get really confusing (implicit type conversions can be a real bitch, sometimes).