Switch

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon May 18 19:39:23 PDT 2009


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.


Andrei



More information about the Digitalmars-d mailing list