Keith Thompson said:
"BartC" <
[email protected]> writes:
statements. Presumably it would test a given value against a
sequence of other values.
That's the main thing.
Would those tests be limited to equality,
or would it permit "<", ranges, and so forth?
Even limiting switch to just matching an integer expression against a number
constant values, there's a bunch of things that are more useful to get
right first:
o Allow case 10,20,30,40,50: instead of case 10: case 20: case 30: case 40:
case 50:
o Allow case 1..5: instead of case 1: case 2: case 3: case 4: case 5:
o Although not so important if the above is present, allow mixed scalars and
ranges: case 1..5,7..10: (but the comma is a problem in C)
o Get rid of the need for break (but that would need an alternative keyword
for switch, and perhaps for case, to form a new syntax where break is not
needed and case labels are well-behaved (not allowed in every conceivable
place).
o Possibly, allow switch to return a value (although I mainly use such a
feature in a streamlined version of switch, where the case values are
omitted and assumed to be (0,) 1, 2, 3 ..., and with an abbreviated syntax.
In C, that might look like:
x = (n ? a, b, a+b : z);
if you excuse the commas again. Assign a, b or a+b to x when n is 0, 1 or 2,
otherwise z. Only one right-side-side value is evaluated).
It would evaluate the given expression only once rather than
re-evaluating it for each test -- but that's trivally accomplished
by using a temporary variable.
Am I missing some significant advantage in expressive power, safety,
performance, or something else that would make a dynamic switch
worth adding to the core language?
I use another version of switch when:
o I don't care whether a jumptable is created
o Integer case values are too big or disparate to form a jump table
o Non-integer case values are used
o Non-constant case values are being tested
That last one is the proposal here, but I have say there is very rarely a
need for that (in my own code); the other reasons are more likely ones to
use this alternate (in my case) form of switch. But why not allow it anyway?
Just to have the 'expressive power' when you need it.