www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - case statements

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
This is probably a bad idea, but from my readings of the dmd source, I
noticed some preprocessor defines that looked useful, along the lines of

#define CASES case A:case B: (etc)

I'd kinda like something similar in D, but a naive attempt with mixins
doesn't work, e.g.

immutable string cases = "case 1: case 2:"; //my d2fu sucks

..

switch(x){
    mixin(cases);
      dosomething();
    default:
      dosomething();
}

Any ideas (not including concatenating cases with body of case)?
Oct 10 2009
next sibling parent Jeremie Pelletier <jeremiep gmail.com> writes:
Ellery Newcomer wrote:
 This is probably a bad idea, but from my readings of the dmd source, I
 noticed some preprocessor defines that looked useful, along the lines of
 
 #define CASES case A:case B: (etc)
 
 I'd kinda like something similar in D, but a naive attempt with mixins
 doesn't work, e.g.
 
 immutable string cases = "case 1: case 2:"; //my d2fu sucks
 
 ..
 
 switch(x){
     mixin(cases);
       dosomething();
     default:
       dosomething();
 }
 
 Any ideas (not including concatenating cases with body of case)?
D supports case ranges: switch(x) { case 1: .. case 10: doSomething(); default: doSomething(); }
Oct 10 2009
prev sibling parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Sat, Oct 10, 2009 at 8:45 PM, Ellery Newcomer
<ellery-newcomer utulsa.edu> wrote:
 This is probably a bad idea, but from my readings of the dmd source, I
 noticed some preprocessor defines that looked useful, along the lines of

 #define CASES case A:case B: (etc)

 I'd kinda like something similar in D, but a naive attempt with mixins
 doesn't work, e.g.

 immutable string cases =3D "case 1: case 2:"; //my d2fu sucks

 ..

 switch(x){
 =A0 =A0mixin(cases);
 =A0 =A0 =A0dosomething();
 =A0 =A0default:
 =A0 =A0 =A0dosomething();
 }

 Any ideas (not including concatenating cases with body of case)?
A single string mixin must consist of an entire, fully-formed statement, expression, or declaration (depending on where it's used). Case labels do not, on their own, count as a statement. In addition, there is a bug that prevents you from string-mixing-in cases inside a switch. For some reason you have to mix in the entire switch statement.
Oct 10 2009
parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
Jarrett Billingsley wrote:
 On Sat, Oct 10, 2009 at 8:45 PM, Ellery Newcomer
 <ellery-newcomer utulsa.edu> wrote:
 This is probably a bad idea
A single string mixin must consist of an entire, fully-formed statement, expression, or declaration (depending on where it's used). Case labels do not, on their own, count as a statement.
Yeah they do. Ever wonder why you can do things like duff's device? Or case 1: case 2: or switch(x){ case 3: }
 
 In addition, there is a bug that prevents you from string-mixing-in
 cases inside a switch. For some reason you have to mix in the entire
 switch statement.
http://d.puremagic.com/issues/show_bug.cgi?id=1534 Hey! there's even a patch for it! And it works! Rainier, you da man!
Oct 10 2009
parent reply Christopher Wright <dhasenan gmail.com> writes:
Ellery Newcomer wrote:
 Jarrett Billingsley wrote:
 On Sat, Oct 10, 2009 at 8:45 PM, Ellery Newcomer
 <ellery-newcomer utulsa.edu> wrote:
 This is probably a bad idea
A single string mixin must consist of an entire, fully-formed statement, expression, or declaration (depending on where it's used). Case labels do not, on their own, count as a statement.
Yeah they do. Ever wonder why you can do things like duff's device? Or
No, you can put multiple case labels together. A case "statement" is just a special category of label.
Oct 10 2009
parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
Christopher Wright wrote:
 Ellery Newcomer wrote:
 Jarrett Billingsley wrote:
 On Sat, Oct 10, 2009 at 8:45 PM, Ellery Newcomer
 <ellery-newcomer utulsa.edu> wrote:
 This is probably a bad idea
A single string mixin must consist of an entire, fully-formed statement, expression, or declaration (depending on where it's used). Case labels do not, on their own, count as a statement.
Yeah they do. Ever wonder why you can do things like duff's device? Or
No, you can put multiple case labels together. A case "statement" is just a special category of label.
We're probably thinking on different levels. I'm down on the syntactic level. parseStatement(flag) can parse 'case x:' and nothing more if there's nothing more.
Oct 10 2009