Java May Get Closures: Functional Programming on the March!

Gilad Bracha has a substantive proposal to add closures to Java (for those familiar with .NET but not Java, this is generally equivalent to adding delegates and C# 2.0's anonymous delegates). The proposal has slightly cleaner syntax than what is in C# but I believe would reprise an issue that C# has, in that "captured outer variables" are shallow copies. This means that when an outer object with reference semantics is closed over, you can end up creating a very thread-hostile functional data structure. Monitor-based thread safety, already a very weak model, is weakened further by the proliferation of implicit objects (i.e., closures that use reference semantics on closed-over outer variables).

Not that any design decision that creates implicit deep copies would be without its issues! Adding closures to the JVM/CLR object model is, like generics, a tough nut to crack. One of the problems I had with the addition of generics to the CLR is that they were absorbed into the standard Common Type System before they had a chance to play out in the real world. One of the great advantages that the JVM and CLR had in their early days was that they very pragmatically reflected lessons learned from real-world uses of languages like C++ (e.g., string representation issues), Delphi (e.g., component models), and Smalltalk (e.g., 'everything is an object'). Necessarily, today's VM advances are less road-driven. While there's nothing wrong with that, I think that standardization of such advances and their implementations should be more cautious.

Update: See my post for an example of the surprising behavior resulting from C#'s design.