digitalmars.D.learn - Switch Statement case '0': .. case '9'
- Paul (15/15) Jun 14 2012 The book in section 3.5 gives these valid case examples:
- Timon Gehr (23/38) Jun 14 2012 case-range expressions don't work with strings. (because there is no
- Paul (3/52) Jun 14 2012 What are those examples I quoted from the book? Aren't they
- bearophile (4/8) Jun 14 2012 Those are char ranges.
- Paul (2/11) Jun 14 2012 Thanks.
The book in section 3.5 gives these valid case examples: case ’0’: .. case ’9’: case ’A’: .. case ’Z’: case ’a’: .. case ’z’: case ’.’, ’,’, ’:’, ’;’, ’!’, ’?’: In my code: case "STEP01": ...compiles and works correct case "STEP01", "STEP02": ...compiles and works correct case "STEP01": .. case "STEP50": ...compiler reports: iccstats.d(27): Error: Integer constant expression expected instead of "STEP01" iccstats.d(27): Error: Integer constant expression expected instead of "STEP50" iccstats.d(27): Error: integral constant must be scalar type, not const(char[]) Help please.
Jun 14 2012
On 06/14/2012 05:59 PM, Paul wrote:The book in section 3.5 gives these valid case examples: case ’0’: .. case ’9’: case ’A’: .. case ’Z’: case ’a’: .. case ’z’: case ’.’, ’,’, ’:’, ’;’, ’!’, ’?’: In my code: case "STEP01": ...compiles and works correct case "STEP01", "STEP02": ...compiles and works correct case "STEP01": .. case "STEP50": ...compiler reports: iccstats.d(27): Error: Integer constant expression expected instead of "STEP01" iccstats.d(27): Error: Integer constant expression expected instead of "STEP50" iccstats.d(27): Error: integral constant must be scalar type, not const(char[]) Help please.case-range expressions don't work with strings. (because there is no general way to meaningfully fill in the gap.) You could automatically generate the case statements via a string mixin (untested): mixin({ string r; foreach(i;1..51) r~=`case "STEP`~(i<10?"0":"")~to!string(i)~`":`; return r; }()); But that is probably overkill, why not just parse the string with an if statement? If you need fallthrough, you can use a goto statement if(...) goto Lstepn; switch(...){ case ...: ... break; Lstepn: case ...: ... break; }
Jun 14 2012
On Thursday, 14 June 2012 at 16:16:19 UTC, Timon Gehr wrote:On 06/14/2012 05:59 PM, Paul wrote:What are those examples I quoted from the book? Aren't they string ranges?The book in section 3.5 gives these valid case examples: case ’0’: .. case ’9’: case ’A’: .. case ’Z’: case ’a’: .. case ’z’: case ’.’, ’,’, ’:’, ’;’, ’!’, ’?’: In my code: case "STEP01": ...compiles and works correct case "STEP01", "STEP02": ...compiles and works correct case "STEP01": .. case "STEP50": ...compiler reports: iccstats.d(27): Error: Integer constant expression expected instead of "STEP01" iccstats.d(27): Error: Integer constant expression expected instead of "STEP50" iccstats.d(27): Error: integral constant must be scalar type, not const(char[]) Help please.case-range expressions don't work with strings. (because there is no general way to meaningfully fill in the gap.) You could automatically generate the case statements via a string mixin (untested): mixin({ string r; foreach(i;1..51) r~=`case "STEP`~(i<10?"0":"")~to!string(i)~`":`; return r; }()); But that is probably overkill, why not just parse the string with an if statement? If you need fallthrough, you can use a goto statement if(...) goto Lstepn; switch(...){ case ...: ... break; Lstepn: case ...: ... break; }case ’0’: .. case ’9’: case ’A’: .. case ’Z’: case ’a’: .. case ’z’:
Jun 14 2012
Paul:What are those examples I quoted from the book? Aren't they string ranges?Those are char ranges. Bye, bearophilecase ’0’: .. case ’9’: case ’A’: .. case ’Z’: case ’a’: .. case ’z’:
Jun 14 2012
On Thursday, 14 June 2012 at 16:49:16 UTC, bearophile wrote:Paul:Thanks.What are those examples I quoted from the book? Aren't they string ranges?Those are char ranges. Bye, bearophilecase ’0’: .. case ’9’: case ’A’: .. case ’Z’: case ’a’: .. case ’z’:
Jun 14 2012