Closures in valid Java, limited to single expressions
Alex Buckley
alex.buckley at oracle.com
Fri Apr 2 12:39:49 PDT 2010
Nice to see a real experiment. What is the type of a 'fn' expression?
How do you type-check the body of the 'fn' expression? How is 'this'
handled in the body?
Alex
On 4/2/2010 3:35 AM, Håkan Råberg wrote:
> I'm one of those who still hold out for closures in Java.
>
> While waiting, I've implemented a port of Ruby's Enumerable module to Java, which supports simple single expression closures, generated using ASM, as part of the library:
>
> int factorial = inject(ints, fn(n, m, n * m));
>
> Map<String, Integer> map = new HashMap<String, Integer>();
> eachWithIndex(strings, fn(s, idx, map.put(s, idx)));
>
> int x = 6;
> fn(n, x += n).call(2);
> assert x == 8;
>
> I do realize that what I've implemented here is far from what has to be done to really add closures to the Java language, and that there are loads of cases that the different proposals address that this obviously don't, and can't, as it has to work with current versions of Java.
>
> Having said that, I'm interested in some feedback from this group.
>
> The project lives at:
> http://github.com/hraberg/enumerable
>
> Some background:
> http://www.jroller.com/ghettoJedi/entry/closures_in_valid_java_limited
>
> cheers, Hakan
>
>
> __________________________________________________
> Använder du Yahoo!?
> Är du trött på spam? Yahoo! E-post har det bästa spamskyddet som finns
> http://se.mail.yahoo.com
More information about the lambda-dev
mailing list