let/const in switch cases

Brendan Eich brendan at mozilla.org
Tue Aug 14 08:50:08 PDT 2012


Andreas Rossberg wrote:
> On 10 August 2012 05:48, Brendan Eich<brendan at mozilla.org>  wrote:
>> Luke Hoban wrote:
>>> Current Chrome builds appear to follow this approach, reporting that
>>> the 'let' in the initial code sample above appears in an 'unprotected
>>> statement context'.
>> Are we all trying to follow the draft spec, or not? Thanks for proposing to
>> change it first. Implementations deviating without talking first =
>> disharmony.
>
> To be fair, IIRC we implemented that before there was much of a spec
> covering details like that. So we picked what we thought makes most
> sense (and is most conservative).

Fair enough.

> Having said that, I fully agree with Luke. The switch statement is
> what it is, unfortunately, and beyond repair,

In a "make it look like Java" regime, it could not be otherwise, 
although it came in a year later, 1996. The alternative of leaving it 
out was at that point "hard" because else-if trees are a pain and retest 
the discriminant. The alternative of different syntax with better 
semantics was out because of (a) the C (C++ -> Java) 
pedagogy/code-reuse/brainprint "gravity well"; (b) still not enough time 
on my side to do better.

ECMA-262 Edition 1 lacked switch but the committee was not in a good 
state to invent, so in '98 and '99, ES3 drafts codified switch with fall 
through, break and continue to label, do-while, try/catch/finally -- all 
pretty safe bets based on Java (some too much so, e.g. instanceof).

> but to maintain scoping
> sanity, we should rule out examples that mistake its body for a proper
> block.

I gave lots of examples (there are many more) of Mozilla JS that uses 
let in switch-case statement lists without issue. That code is not 
insane, and it doesn't need braces. But if TC39 agrees to require 
braces, some poor soul is gonna have to go waste some minutes to hours 
adding them. Seems a shame.

> There is precedent for that in C++, where you cannot place non-trivial
> declarations into a switch block either.

C++ broke Duff's-device use-cases where in C one can declare in the 
braced body of the switch, yes. Is C++ the exemplar here?

If we expect guards on declarations, perhaps so. I can give this one up, 
but againt the people who will have to add gratuitous braces to extant 
JS code will want your head and mine, for a few minutes anyway.

/be



More information about the es-discuss mailing list