D mixins with string operators

I have a D mixin that I would like to use to generate a switch statement (case values, in particular) for string values, but despite the fact that KeyValues has entries in it and provides the correct key values, the default case is always executed only one:

 class DataStore(KeyValues...) { void stringSetData(string key, string data) { switch(key) { foreach(D; KeyValues) { mixin("case \"" ~ D.Name ~ "\": set(to!(D.Type)(data)); break;"); } default: throw new Exception("Invalid meta key"); break; } } } 

I tested this with hard-coded values ​​and it works as expected, so my suspicion is that I might have something wrong with my mix. How can I make this work as expected?

+4
source share
1 answer

break inside the mix breaks from the foreach , not the switch . Replace it with return or labeled break .

By the way, if you try to compile this code with warnings turned on, you will get some strange error messages from DMD.

+5
source

All Articles