case statements

Jeremie Pelletier jeremiep at gmail.com
Sat Oct 10 19:32:36 PDT 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();
}


More information about the Digitalmars-d-learn mailing list