Closures in valid Java, limited to single expressions
Neal Gafter
neal at gafter.com
Fri Apr 2 12:56:01 PDT 2010
See http://github.com/hraberg/enumerable/blob/master/src/main/java/lambda/Lambda.java
for some insight into how this is type checked. In the expression
fn(n, m, n*m), n is lambda.Lambda.n, m is lambda.Lambda.m, and fn is
lambda.Lambda.fn, all available due to a static import. It's all
"woven" into an object creation by bytecode rewriting.
On Fri, Apr 2, 2010 at 12:39 PM, Alex Buckley <alex.buckley at oracle.com> wrote:
> 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