
Java Closures after all? - fogus
http://puredanger.com/tech/2009/11/18/closures-after-all/
======
fadmmatt
Don't forget that Java already has anonymous classes, which can be used to
mimic closures.

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.

~~~
statictype
While technically correct, using anonymous inner classes makes code more
verbose. Closures are a more elegant solution.

If you look at it that way, anything from generators to continuations can be
'simulated' using enough layers of class definitions.

~~~
trapper
<http://code.google.com/p/lambda4jdt/>

------
bokchoi
Apparently the closures will be based on the FCM (first class methods)
proposal be Stephen Colebourne:

<http://docs.google.com/Doc?id=ddhp95vd_0f7mcns>

~~~
akeefer
First-class methods would be a win, but the syntax leaves a little bit to be
desired. Closures would be much more of a syntactic eyesore if they A) had
context-based type inference on the arguments, so that if I'm passing a
closure to a method that expects (String, String) -> as its type I don't need
to redeclare the variable types and B) allowed for embedded expressions rather
than requiring them to be full function definitions with a "return" statement
in there. Unfortunately, the FCM proposal doesn't allow either of those.

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.

------
natmaster
Maybe I'm missing something here: but what's the point of closures without
functions as first class variables?

~~~
jshen
<http://docs.google.com/Doc?id=ddhp95vd_6hg3qhc>

Scroll down to method literals

------
jcapote
Something tells me these are going to be as bad as PHP's closures; you can't
just tack on lambdas at the last minute, it has to be part of the language
design initially (no?)

~~~
kingkilr
Python had them tacked on after the fact (version 2.0 as I recall, or was it
2.2?), they work pretty nicely IMO, as long as you treat functions as first
class objects the whole time you're ok.

~~~
dantheman
Python lambdas do not work well, in fact they can only be one line long. For
some cases this may be ok, especially if python was were the programmer
learned about lambdas, but from those who come from with languages with real
lambdas it is quite limiting.

~~~
kingkilr
We aren't talking about lambdas, we are talking about closures. Lambdas are
simple syntactic sugar for a function definition that has a single expression,
closures are a concept to capture the outer scope.

