lexical for-in/for-of loose end

Herby Vojčík herby at mailbox.sk
Sat Feb 4 13:53:58 PST 2012

It looks to me that all those schemes and desugaring are very 
complicated. What is bad with scenario of reusing the same variable and 
isolating it as local only for the async closures?

The code:

   for (let a=..., b=..., otherInit; cond; update) {
     // use a, b and things from otherInit

Now, if you allow first class refs _in_implementation_, it could be 
something like this:

   let a=..., b=..., otherInit;
   while (cond) {
       let _inner_a, _inner_b; // undefined as yet;
       let ref a :- ref outer a, ref b :- ref outer b, ...; // (1)
       try {
         // use a, b and things from otherInit

         // names a, b and others from for-let are compiled using
         // refs defined in (1) which point to outer
         // variables while processing loop body
       } finally {
         _inner_a = a; _inner_b = b; ... // assign actual values
         ref a :- ref _inner_a; ref b :- ref _inner_b; ...
         // fixing refs at the end of the body to local ones
         // async closures will use these (since they use the refs
         // which were rerouted)
         // the same closures when executed inside loop body would
         // of course use the outer shared variables, since fixing
         // only happens after the body
         // It is always the same ref, but points to different things
         // in loop and out of the loop

This way, inside the loop, always the same (outer) variables are used, 
any inc() things will act upon them, no copying in-out.
At the end of every iteration, the refs that pointed to these shared 
variables are fixed to inner ones, using actual values.


More information about the es-discuss mailing list