Java Closures after all?
puredanger.com
puredanger.com
I taught my advanced compilers class how to compile Scheme directly to Java by using them to implement lambda:
http://matt.might.net/articles/compiling-to-java/
In fact, as the link above points out, anonymous classes are flexible enough to express the Y combinator, allowing "recursion-less recursion" in Java.
If you look at it that way, anything from generators to continuations can be 'simulated' using enough layers of class definitions.
To use their example, it should be much more like:
Collections.sort(list, \str1, str2 -> str1.length() - str2.length());
instead of:
Collections.sort(list, #(String str1, String str2) { return str1.length() - str2.length(); });
There's no technical reason you couldn't do the former in Java (and their proposal seems to infer the return type just fine?), but no one involved with the Java language seems to really value conciseness.
http://www.javac.info/closures-v06a.html http://www.jroller.com/scolebourne/entry/closures_in_jdk_7
Scroll down to method literals
Clarify this? Also, isn't there some inconvenience due to the indentation thing?
Scheme avoids the issue by having a non-ambiguous syntax (a big advantage of those parens), and Lua avoids it by defaulting to global scope* unless you declare them with "local", which also anchors their scope unambiguously.
* Which is actually not a big deal - the global table is just another table, so you can hook its set method to catch unexpected globals (see strict.lua). It's far less trouble than Python's solution.
i.e, there was issues with scoping that had nothing to do with closures per-se. When they fixed it, closures automatically became possible.
That said, the BGGA proposal is a bit of a disaster in my opinion: the distinction between "restricted" and "unrestricted" closures, along with non-local returns, just makes no sense. My assumption is that they want to use closures for things like control structures, which they're just totally inappropriate for, rather than as just anonymous functions, which is what they really ought to be.
Basically, it's what they did with generics: take a relatively sensible concept, then add a bunch of baroque edges to it for no good reason. Honestly: => and ==> are almost equivalent things with totally different semantics as far as control structures? Really?
People are going to understand that about as well as they understand why they can't add an Object to List<? extends Object>, or why they can't pass a List<String> back from a method that says it returns List<Object>. By which I mean, not at all.
Despite this principle, Java's users asked for generics, and so the language designers added them. But with a twist; they intentionally made the simple concept very confusing. This is so that the next time someone asks for a feature, they can point at generics and say "look at how badly we broke that! do you really want us adding more features?" A few episodes like this (closures will be next), and people won't ask for features anymore. With no more feature requests, it will become clear that Java Is Perfect, and the designers can retire with a very smug feeling of self-superiority.
I think.
The BGGA proposal was recently updated: "The concepts of restricted and unrestricted function types and closures have been removed. Now, all lambda expressions are restricted. The equivalent of the previous specification's unrestricted closure may be passed to a method by using the control invocation syntax."
It is rumored that it is something like a compromise between the BGGA and the FCM proposals that Sun is pushing.