Another switch
Not sure whether this will be of interest to you, but I have been working on a JS-derived language called Proto (still highly experimental) which has a switch statement that works exactly as you described:
Nathan-Wall/proto/blob/master/docs/control/switch.md
Perhaps you will at least find it interesting. :)
Nathan
Definitely good to see new languages being designed and implemented.
JS is not going to break compatibility on the old fall-through behavior of switch, inherited from Java from C++ from C. All the C-like languages copy this flaw, because to do otherwise with the same keyword would be worse (confused users cross-training and -coding among languages would want our scalps), and IMHO using novel reserved words would be hardly better.
yes, so great it would be a pleasure to contribute :)
the proposal doesn't want, by no means, to break the compability with the present syntax and/or semantics of the switch. At most, should be considered as an extension of current syntax with a consequential new semantics.
the swith, as it is known, should anyhow be written as
switch (...) {
case ...: ...; break;
case ...: ...; break;
case ...: ...;
case ...: ...; break;
otherwise: ...
}
but, it could be handy to write the same thing with a slightly different syntax:
switch (...) break {
case ...: ...;
case ...: ...;
case ...: ...; continue;
case ...: ...;
otherwise: ...
}
Some break less, no new keyword, no incompatibility with past. Who shall use it, will know from the start that here, the continue will not lead him at the beginning of the first for or while that englobe the switch, but only at the following case.
Then, willingly, we could also think that normal switch be desugared in
switch (...) continue {
case ...: ...; break;
case ...: ...; break;
case ...: ...;
case ...: ...; break;
otherwise: ...
}
with break and continue (except that between ')' and '{' ) that, obviously, continue as before. This isn't a must, it's just for my pleasure in finding regularity.
At last, if we were to look for the burden of a new keyword, even better
select (...) {
case ...: ...;
case ...: ...;
case ...: ...; continue;
case ...: ...;
otherwise: ...
}
but I realize this could be a break point of the existing code and, onestly speaking, it is beyond my ability to evaluate.
Object literals are already a great alternative to switch in JS:
var cases = {
val1: function () {},
val2: function () {}
};
cases[val]();
Fall through is more trouble than it's worth, IMO.
In that case, you’d need a hasOwnProperty
check to make sure you’re not trying to call __proto__
or toString
, etc. See rwaldron/idiomatic.js/#misc for a more complete example.
Also only works when you're switching on something with a meaningful conversion to string.
On Fri, Feb 21, 2014 at 7:55 AM, Nick Krempel <ndkrempel at google.com> wrote:
Also only works when you're switching on something with a meaningful conversion to string.
On 20 Feb 2014, at 21:20, Eric Elliott <eric at ericleads.com> wrote:
Object literals are already a great alternative to switch in JS:
Right, this wouldn't work if the "case" wanted object references, but it would work nicely with Symbols.
In practice, I find that everything converts nicely to a string when you precede it with a ternary assignment.
I also find that when you do that, it's pretty trivial to control what
those strings are, which makes hasOwnProperty
superfluous.
I haven't used a switch in JavaScript for quite a few years now, and I don't miss it at all.
In the ES6 world, you should probably set up a Map for your switch statement; that would allow you to easily use non-string cases.
I wish to submit a little proposal.
Today the switch statement has an explicit break at the end of the statement and an implicit continue to the next case
but this break is very boring and error prone.
Wouldn’t it be possible to think a switch that has an explicit continue to the next case and an implicit break at the end of the statement?
This is the hypothetical new statement syntax with a new keyword:
or without a new keyword:
but with the current switch equals to