return when desugaring to closures

Mark S. Miller erights at google.com
Mon Oct 13 08:43:38 PDT 2008


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? 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. If we can
agree quickly on per-iteration, then

    for (const x = ...) {...x...}

should be allowed in ES3.1 (whether or not const hoists to block
start). After ES3.1

    for (const i :T[i] = ...) {...; a[i] = function(){...i...}; ...}

would then mean what it should mean. Cool.

-- 
    Cheers,
    --MarkM


More information about the Es-discuss mailing list