Sugaring the switch()
2008/9/6 Michael Haufe <TNO at thenewobjective.com>:
I didn't see this mentioned yet, so I'd like to propose implementing a feature available in VB.NET that I think would be valuable in ES. Some sugar for the switch() block:
switch(foo){ case 1,2,3: //stuff... break; case a,b,c: //stuff... break; }
This is already valid ECMAScript 3. Run the following in any browser and see:
var
t=2;
switch(t){
case 1,2,3:
alert('1,2,3');
break;
default:
alert('default');
break;
}
As you can see, the "1,2,3" part is treated as an expression list, that is each expression is executed in order and the result of the last one is the return value of the whole expression list. In other words that is equivalent to
var
t=2;
switch(t){
case 3:
alert('3');
break;
default:
alert('default');
break;
}
Would be equivalent to:
switch(foo){ case 1: //stuff... break; case 2: //stuff... break; case 3: //stuff... break; case a: //stuff... break; case b: //stuff... break; case c: //stuff... break; }
This would be a backwards incompatible change, and as such is a no-fly. I don't think changing semantics for existing established syntax will be an option. If you want to introduce new semantics, you need to make them either take place in normal host objects or add specific new syntactic forms for those semantics.
What's wrong with the current syntax:
switch (foo) { case 1: case 2: case 3: // stuff break;
case 4:
case 5:
// more stuff
break;
}
I'll agree it's not as compact, but as David pointed out the comma
version is already valid with an alternate meaning.
Michael Haufe wrote:
I didn't see this mentioned yet, so I'd like to propose implementing a feature available in VB.NET that I think would be valuable in ES. Some sugar for the switch() block:
switch(foo){ case 1,2,3: //stuff... break; case a,b,c: //stuff... break; }
Same as:
switch(foo){ case 1: case 2: case 3: //stuff... break; case a: case b: case c: //stuff... break; }
Michael Haufe wrote:
I didn't see this mentioned yet, so I'd like to propose implementing a feature available in VB.NET that I think would be valuable in ES. Some sugar for the switch() block:
switch(foo){ case 1,2,3: //stuff... break; case a,b,c: //stuff... break; }
That syntax can't be used because a case label is an arbitrary expression, so it would be interpreted as a comma expression.
Would be equivalent to:
switch(foo){ case 1: //stuff... break; case 2: //stuff... break; case 3: //stuff... break; case a: //stuff... break; case b: //stuff... break; case c: //stuff... break; }
What is wrong with:
switch (foo) { case 1: case 2: case 3: // stuff... break; case a: case b: case c: // stuff... break; }
?
The only reason for the suggestion was to save in typing, code size and to show what seems to me to be a better visual organization of larger lists of comparisons. liorean's example clarified the point pretty well as to why my suggestion wouldn't be worth the cost due to the change in meaning of the expression list.
Due to the ordering requirement of the list as he had shown, its no wonder I've never gotten such a pattern to work as expected. ;)
I didn't see this mentioned yet, so I'd like to propose implementing a feature available in VB.NET that I think would be valuable in ES. Some sugar for the switch() block:
switch(foo){ case 1,2,3: //stuff... break; case a,b,c: //stuff... break; }
Would be equivalent to:
switch(foo){ case 1: //stuff... break; case 2: //stuff... break; case 3: //stuff... break; case a: //stuff... break; case b: //stuff... break; case c: //stuff... break; }