Suggestion: new switch statement

nobody nobody at mailinator.com
Mon Aug 21 07:49:40 PDT 2006


Jeff wrote:
> How about allowing:
> 
> switch (val) {
>     case (1) {
>     doX();
>     } case (2, 3) {
>         doY();
>     } default {
>         doZ();
>     }
> )
> 
> Or would this create some horrible syntactic ambiguities? Or, on the 
> other hand, it could just be too damn ugly. ;)

The answer is that you can do interesting things in switch statements. Probably 
the most famous example is Duff's Device which you can see an example of in the 
Mixin section of the D docs:

http://www.digitalmars.com/d/mixin.html



template duffs_device(alias id1, alias id2, alias s)
{
     void duff_loop()
     {
	if (id1 < id2)
	{
	    typeof(id1) n = (id2 - id1 + 7) / 8;
	    switch ((id2 - id1) % 8)
	    {
		case 0:        do {  s();
		case 7:              s();
		case 6:              s();
		case 5:              s();
		case 4:              s();
		case 3:              s();
		case 2:              s();
		case 1:              s();
				  } while (--n > 0);
	    }
	}
     }
}



More information about the Digitalmars-d mailing list