What's a good approach to DRY with the block code of a case-statement?

z z at z.com
Thu Apr 29 05:46:23 UTC 2021


I'd recommend you to use templates with alias parameters but you 
mentioned that you need to retain function context(for gotos, 
continue, break, etc...)
One thing you could do is mix the ugly mixin solution with the 
good one:
```D
//inside the function, so that you can access locals
pragma(inline, true)
string getDRYstr(T, parameters...)() {
     static assert(__ctfe);
     string mixedinstr;
     mixedinstr ~= /*code*/;
     static if(/**/){mixedinstr ~= /*code*/;}
     //...
     mixedinstr ~= "break;";
     return mixedinstr
}
```
There is no doubt that it is an ugly solution but the time saved 
not copy-pasting code could be worth it.


More information about the Digitalmars-d-learn mailing list