Closures in valid Java, limited to single expressions
Håkan Råberg
hraberg at yahoo.se
Fri Apr 2 14:41:32 PDT 2010
Glad you're enjoying reading the code. I take lightweight as a compliment.
Neal sums up how it works it works nicely. Basically the type of the "closure" expression itself as passed into fn is R, which returns a Fn<R>.
"this" is the outer this, there's currently no way to refer to the inner one, and I'm not sure if it's useful as the closures can only be one expression. (So the term "body" may be generous!)
An unsolvable (?) problem with this approach is that you cannot deal with void. It was pure luck that printf turned out to return a PrintStream.
I'm happy to answer more questions about how it works and any suggestions on how to make it better. I'm currently trying to get my head around the different proposals and their differences, and how they deal with primitives for example.
cheers, Hakan
--- Den fre 2010-04-02 skrev Alex Buckley <alex.buckley at oracle.com>:
> Från: Alex Buckley <alex.buckley at oracle.com>
> Ämne: Re: Closures in valid Java, limited to single expressions
> Till: lambda-dev at openjdk.java.net
> Datum: fredag 2 april 2010 21.06
> Fair enough. It's lightweight, I'll
> give it that. And reading this code
> is much more fun than the other thing I am reading at the
> moment.
>
> Alex
>
> On 4/2/2010 12:56 PM, Neal Gafter wrote:
> > 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
> >>
> >>
>
>
__________________________________________________
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