For Loop Desugaring (was: return when desugaring to closures)
Waldemar Horwat
waldemar at google.com
Tue Oct 14 12:39:51 PDT 2008
Jon Zeppieri wrote:
> On Mon, Oct 13, 2008 at 10:48 PM, Mark S. Miller <erights at google.com> wrote:
>> You're right. However, the desugaring is more complex than I expected.
>> Thanks for asking me to write it down.
>>
>> for (<keyword> <varName> = <initExpr>; <testExpr>; <updateExpr>) { <body> }
>>
>> desugars to (hygienic renaming aside):
>>
>> breakTarget: {
>> const loop = lambda(iter = <initExpr>) {
>> <keyword> <varName> = iter;
>> if (! <testExpr>) { break breakTarget; }
>> continueTarget: { <body> }
>> lambda(iter2 = <varName>) {
>> <keyword> <varName> = iter2;
>> <updateExpr>;
>> loop(<varName>);
>> }();
>> };
>> loop();
>> }
>>
>> I believe this meets all your requirements.
>
> I believe it does. Very cool. It won't handle the fully general
> for(;;). E.g.,
>
> for (let fn = lambda(n) { ... fn(...) ... }; <testExpr>; <updateExpr>) ...
Yeah, that's a problem with the current formulation.
> I also find it odd that
>
> {
> let x = 0;
> for (; x < n; x++) ...
> }
>
> should have different behavior than
>
> for (let x = 0; x < n; x++) ...
I do too.
Another issue with the rewrite is that it interacts badly with getters and setters on the iteration variable.
Waldemar
PS. What does lambda(x = y){...}() mean? Is it different from lambda(x){...}(y)? What does lambda(x = y){...}(z) do when z is undefined?
More information about the Es-discuss
mailing list