Pure Danger Tech


navigation
home

To closure or not to closure

15 Dec 2007

I blogged some thoughts on the direction of Java yesterday and Dario Laverde responded with a lot of interesting thoughts, which got me thinking again about closures. I’ve found myself moving back and forth on whether I want them in Java or not. I often see use cases where a closure is exactly what I want and the alternatives in current Java are really ugly. I know that if I had closures I could express exactly what I want, much more precisely.

But, I inevitably compare to generics. I really like generics when using APIs as they give you a much more precise interface. I don’t particularly enjoy the implementation side of the API, particularly when you hit the frustrating “Wall of Erasure”. I think on balance, I could do without them (in erased form). The annoyance of using them is too high given the (relatively small) benefits of type precision.

Closures, especially the control abstraction aspects, have the power to really transform the code we write and look at every day. I know some of those transformations would be really helpful, but I worry that like generics, in other fairly common circumstances they’ll be really ugly and hard to understand. And then I think that maybe it’s just too much for Java and we should use a different language instead if we need that power.

In any case, I think from a language perspective, this is the biggest question before us in the Java community and the Java Modules in JSR 277/294 are the biggest library question. Both of these stand to have a huge impact on Java but its still unclear whether that impact will be a net positive or negative.