for-loops and declaration-like init expressions

Will Ray wray167 at gmail.com
Thu Jun 5 08:32:00 PDT 2014


Mark, could you explain how the condition and iteration statements in the
for loop interact with such an initialization? I agree that banning these
seems like the clearer way to go.


On Thu, Jun 5, 2014 at 10:17 AM, Mark S. Miller <erights at google.com> wrote:

> Why not accept these as for-loop initializations, so that x actually has
> that function and that class as its initial value in the first iteration of
> the loop? To me, that's the least surprise. Since this is a position in
> which some declarations are accepted, anything there that looks like a
> declaration should be a declaration.
>
> However, I agree that banning these is much less surprising than allowing
> them as expressions rather than declarations.
>
>
>
> On Thu, Jun 5, 2014 at 7:58 AM, Andreas Rossberg <rossberg at google.com>
> wrote:
>
>> C-style for-loops allow declarations as init statements, but only some
>> of them. Yet, the others (function and class) are actually
>> syntactically legal in that position as well, because they are simply
>> parsed as expressions. Consider:
>>
>>   let x = 0
>>   for (let x = 1; ;) x  // 1
>>   for (const x = 1; ;) x  // 1
>>   for (function x(){}; ;) x  // 0
>>   for (class x(){}; ;) x  // 0
>>
>> I think these latter two examples violate the principle of least
>> surprise. I wonder if it wouldn't be cleaner to rule them out, by
>> imposing the same lookahead restrictions on for-loop init expressions
>> as there are for expression statements.
>>
>> The one caveat is that for function, that would actually be a breaking
>> change, but is it likely to be a real world one?
>>
>> What do you think?
>>
>> /Andreas
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>
>
>
> --
>     Cheers,
>     --MarkM
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140605/da511b9d/attachment.html>


More information about the es-discuss mailing list