lexical for-in/for-of loose end

Domenic Denicola domenic at domenicdenicola.com
Tue Feb 7 05:39:14 PST 2012


This seems like a highly compelling argument. I hope I'm not the only one who thinks the existing behavior of `for(;;)` makes sense. Granted, that comes from understanding the detail that closures close over variables and not values, which most users will not. But in general, `for(;;)` makes it very clear that there's a single mutating variable, which the desugarings proposed here seem to overcomplicate and contradict.

As exemplified by the comments to Eric's blog post, the current behavior is not very astonishing to many developers, and once it's explained once, becomes intuitive. (Yes, sampling bias, but still.) The question is whether saving that pedagogic burden is worthwhile.

I argue that it isn't. `for(;;)` is a low-level construct even in ES5, where `Array.prototype.forEach` supplants it for the most part. It doesn't have to do what I mean; it should do what I say. Introducing an extra "set of curly braces" around the loop so that my `let` loop variables don't get hoisted to the outer scope is the most work that makes sense to me.

-Domenic

-----Original Message-----
From: es-discuss-bounces at mozilla.org [mailto:es-discuss-bounces at mozilla.org] On Behalf Of Andy Wingo
Sent: Tuesday, February 07, 2012 5:15
To: es-discuss at mozilla.org
Subject: Re: lexical for-in/for-of loose end

Hi Allen,

On Mon, 2012-02-06 at 11:08 -0800, Allen Wirfs-Brock wrote:
> We're putting a lot of energy into trying to figure out how to "fix"
> for(let;;) when it probably shouldn't even be the preferred form of 
> numeric looping going forward. If we can make for-in/for-of attractive 
> enough then it is less clear why we need to fix for(;;)
[...]
> Maybe don't even add let/const forms to for(;;).

Just as food for thought, here's a C# designer on why they decided to leave "for (int i=0; i<N; i++)" alone, when they decided to make "for (int i in L)" bind a fresh "i":

        We have this same problem in "for" blocks, but "for" blocks are
        much looser about what "the loop variable" is; there can be more
        than one variable declared in the for loop header, it can be
        incremented in odd ways, and it seems implausible that people
        would consider each iteration of the "for" loop to contain a
        fresh crop of variables. When you say for(int i; i < 10; i += 1)
        it seems dead obvious that the "i += 1" means "increment the
        loop variable" and that there is one loop variable for the whole
        loop, not a new fresh variable "i" every time through! We
        certainly would not make this proposed change apply to "for"
        loops.
        
        http://blogs.msdn.com/b/ericlippert/archive/2009/11/12/closing-over-the-loop-variable-considered-harmful.aspx

Regards,

Andy
_______________________________________________
es-discuss mailing list
es-discuss at mozilla.org
https://mail.mozilla.org/listinfo/es-discuss




More information about the es-discuss mailing list