Andrei Alexandrescu wrote:
Georg Wrede wrote:
bearophile wrote:
void classify(char c) {
write("You passed ");
switch (c) {
case '#':
writeln("a hash sign.");
break;
case '0' ..> '9':
writeln("a digit.");
break;
case 'A' ..> 'Z', 'a' ..> 'z':
writeln("an ASCII character.");
break;
case '.', ',', ':', ';', '!', '?':
writeln("a punctuation mark.");
break;
default:
writeln("quite a character!");
break;
}
}
(A bunch of other versions omitted.)...
void classify(char c) {
write("You passed ");
switch (c) {
case '#':
writeln("a hash sign.");
break;
case '0' .. case '9':
writeln("a digit.");
break;
case 'A' .. case 'Z':
case 'a' .. case 'z':
writeln("an ASCII character.");
break;
case '.', ',', ':', ';', '!', '?':
writeln("a punctuation mark.");
break;
default:
writeln("quite a character!");
break;
}
}
This is usable, easy to read -- and the programmer has no problem to
remember that .. works differently in case statements than in ranges.
I'd like to keep the (non-required) colon after the first expression in
a ".." pair of case labels, that is:
case '0': .. case '9':
as opposed to
case '0' .. case '9':
That way it is abundantly clear that the notation has nothing in common
with expression1 .. expression2. The error message if someone forgot the
':' can easily be made clear.
So concatenating ranges would be
case '0': .. case '9': case 'a': .. case 'z':
do something
which then could be written, depending on programmer preferences
case '0':
.. case '9':
case 'a':
.. case 'z':
do something
or
case '0': .. case '9':
case 'a': .. case 'z':
do something
While I'd prefer to omit the colon, I can live with it.