redundant C-style switch; (was: simple shorter function syntax)

Daniel Friesen lists at
Tue Jul 27 21:33:01 PDT 2010

Dmitry A. Soshnikov wrote:
> ...
> Also I think JS still has some redundant C-syntax garbage. For 
> example, that "break" keyword each time in "case" of a "switch" -- for 
> what it is there? For what to write several times "case-case-case" if 
> it's possible to write it once and separate testing operands with a 
> colon? Moreover, this "switch" construct is non-logical in control 
> flow if to place "default" not in the end or to forget this "break" 
> garbage. Why on earth if I have x = 5 and "case 5:" should I catch 
> also "case 6:" if I forgot that useless "break" garbage there? So I 
> like better Ruby's or Coffee's way, e.g. 
> Btw, there're many 
> good syntax sugars which Harmony can borrow (I know some of them are 
> already planned and it's very good, but maybe others too).
> ...
> Dmitry.
Since you pointed out the redundant switch style, that too has been 
bothering me. Personally I have one issue with ruby's case/with, you can 
easily write out multiple with statements in case style with no warning 
thinking they work case style... I was bitten by that once, didn't 
realize for a while that portions of my code weren't being executed.

Personally, my ideal switch would be something like:

	switch(variable) {
		case "a", "A", "Aa": { // instead of break; we use a block
			print("- Alpha");
		case "b", "B", "Beta": print("- B"); // Like if and other things lack of {} only applies to a single expression and breaks out.
		case "g", "G": {
			print("- Gamma");
			break; // leaves the switch
		case "o", "O": {
			print("- Omega");
			continue "a"; // instead of ugly fallthrough case logic continue could be used to jump to the other block
			              // Though, considering `continue foo;` ambiguity I'd expect this would probably require a slightly different syntax `continue blockname, "a";` or something.

The idea came to me after a small misunderstanding about Java's 
switch/case... I was looking at Rhino's source code and it had code 
style with switch/case which may have lacked break only due to the use 
of return, but it did give me ideas, and there have been various times 
while practically coding switch statements where I really wished instead 
of using ugly break-less case logic I could use continue, so those 
together lead to this syntax idea.

Of course given backwards compatibility (in terms of not suddenly making 
existing switch/case code break), and implementation burdens, and taking 
a bit of inspiration from Ruby I think this format would be a good 
revised version of that with a bit of even better shortening.

	case(variable) {
		"a", "A", "Aa": {
			print("- Alpha");
		"b", "B", "Beta": print("- B"); // Like if and other things lack of {} only applies to a single expression and breaks out.
		"g", "G": {
			print("- Gamma");
			break; // leaves the switch
		"o", "O": {
			print("- Omega");
			continue blockname, "a";

case isn't valid outside of switch, so all you'd have to do is check if 
it is directly followed by ( and if so switch to the new syntax. You get 
an extra advantage of eliminating unnecessary case keywords, and also 
end up with a syntax mildly resembling that of a object literal that 
aligns nicely with case.

continue is key though, adding it preserves abilities lost by the 
conversion from swtich/case and also improves what you can do with the 
statement logic.

~Daniel Friesen (Dantman, Nadir-Seen-Fire) []

More information about the es-discuss mailing list