for-loop abstraction: How's this magic gonna work?
Peter Levart
peter.levart at marand.si
Tue Jan 5 05:53:19 PST 2010
On Tuesday 05 January 2010 14:19:34 Gernot Neppert wrote:
> we'd just need to do a "import static Utils.*;" to get the method into scope.
> I'm asking: how is the compiler de-sugaring this invocation:
>
> for eachEntry(String name, Integer value : map) { }
>
> into an iteration over the map?
> Please note that only the definition of the method body contains the
> code that does the actual iteration.
>
I think that the specification is clear about that.
The following example:
for eachEntry(String name, Integer value : map) { ... }
Is de-sugared into the following standard method invocation:
eachEntry(map, #(String name, Integer value)(...; (Void)null));
So the syntax is similar to standard Java control constructs (for, if, while, etc...) but it actualy translates to method invocation where the body of the controled block becomes the body of the lambda expression which is passed as the last argument to the method.
The "for" method modifier is a signal for the compiler to treat the break/continue statements specified in the body of the controlled block differently then they would normally be treated in a transparent lambda - they transfer control either to the end of the controlled block (continue) or to the end of the invoked "for" method (break).
Peter
More information about the closures-dev
mailing list