lexical for-in/for-of loose end

Andreas Rossberg rossberg at google.com
Mon Feb 6 07:26:54 PST 2012


On 4 February 2012 18:49, Brendan Eich <brendan at mozilla.org> wrote:
> I agree we want to capture the first-iteration bindings in any closures in
> those declarators' initializers.
>
> This requires unrolling the loop once. Let's see how the desugaring from:
>
>  for (let d1 = e1, ... dN = eN; cond; update) {
>    body;
>  }
>
> looks. It doesn't seem terrible:
>
>  $loopEnd: {
>    let d1 = e1, ... dN = eN;
>    if (cond) {
>      body;
>      update;
>      const $loop = { |d1, ... dN|
>        if (!cond) break $loopEnd;
>        body;
>        update;
>        $loop(d1, ... dN);
>      }
>      $loop(d1, ... dN);
>    }
>  }

I'm at a loss what this is trying to achieve. Modifications of the
loop variables by the loop _body_ will be visible to future iterations
if they happen in the first, but not if they happen in any consecutive
iteration? That seems odd. What is the use case for this, and why
should we support it as a special case?

In other words, why isn't the following good enough?

{
 let d1 = e1, ..., dN = eN;
 const $loop = { |d1, ..., dN|
   if (cond) {
     body
     update;
     $loop(d1, ..., dN);
   }
 }
 $loop(d1, ..., dN);
}

FWIW, this is simply the generalization of Mark's desugaring so that
it works with destructuring, multiple bindings, and recursive
bindings. Plus, it removes the redundant second lambda in his version.

/Andreas


More information about the es-discuss mailing list