Switch

Denis Koroskin 2korden at gmail.com
Tue May 19 02:08:41 PDT 2009


On Tue, 19 May 2009 06:39:23 +0400, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> 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.
>
>
> Andrei

Will it be possible to write like this?

void classify(char c) {
   write("You passed ");
   switch (c) {
      case '0':
      ..
      case '9':
         writeln("a digit.");
         break;
      case 'A':
      ..
      case 'Z':
         writeln("upper case ASCII character.");
         break;
      case 'a':
      ..
      case 'z':
         writeln("lower case ASCII character.");
         break;
      case '.', ',', ':', ';', '!', '?':
         writeln("a punctuation mark.");
         break;
      default:
         writeln("quite a character!");
         break;
   }
}

Looks cool!



More information about the Digitalmars-d mailing list