return when desugaring to closures

Jon Zeppieri jaz at bu.edu
Mon Oct 13 09:13:12 PDT 2008


On Mon, Oct 13, 2008 at 11:43 AM, Mark S. Miller <erights at google.com> wrote:
> On Mon, Oct 13, 2008 at 8:05 AM, David-Sarah Hopwood
> <david.hopwood at industrial-designers.co.uk> wrote:
>> "for (var ...)" implicitly introduces a block whether or not it
>> is a substatement. This is a wart of C++/C99/Java syntax that
>> we have to live with, since too much code relies on it.
>
> Yes, but how do we live with it? The only currently allowed case,
> "var" as above, is no problem, since it hoists to the function body
> anyway. But what about "let". Do we all agree that in
>
>    for (let x = ...) {...x...}
>    ... x ...
>
> the x after the for loop does not refer to the x defined by the for
> loop?


Yes.  Definitely.


> In that case, no problem with for being a SubStatement.
>
> A remaining interesting question is whether the for loop reassigns to
> a single per-for-loop-entry x, or whether it initializes a fresh
> per-iteration x. If the for loop body has a closure that captures x,
> it makes a difference. I recommend the per-iteration view.

I like your intention here -- I brought up the iteration variable /
closure issue earlier in the thread -- but this seems rather messy.

In the 'var' case, x must be the same throughout, no?

function foo() {
  for (var x = ... ) { ... x ... }
  ... x ...
}

But then it becomes extremely awkward for the 'let' or 'const' case to
use a fresh variable on every iteration.  It ties the semantics of
'for' to its initialization clause in a very strange way.

-Jon


More information about the Es-discuss mailing list