Thread overview
case statements
Oct 11, 2009
Ellery Newcomer
Oct 11, 2009
Jeremie Pelletier
Oct 11, 2009
Ellery Newcomer
Oct 11, 2009
Christopher Wright
Oct 11, 2009
Ellery Newcomer
October 11, 2009
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)?
October 11, 2009
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();
}
October 11, 2009
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 = "case 1: case 2:"; //my d2fu sucks
>
> ..
>
> switch(x){
>    mixin(cases);
>      dosomething();
>    default:
>      dosomething();
> }
>
> 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.
October 11, 2009
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!
October 11, 2009
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.
October 11, 2009
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.