A slightly different opinion of David:
//The actual building of the struct mixin template PrefilledImpl(string name, string common, string individual) { mixin("struct " ~ name ~ "{" ~ common ~ individual ~ "}"); } //A sort of template currying mixin template Prefilled(string templateName, string common) { mixin("mixin template " ~ templateName ~ "(string name, string individual) {mixin PrefilledImpl!(name, \"" ~ common ~ "\", individual);}"); } //declare the "prototype" for the struct mixin Prefilled!("StructWithInt", q{int a;}); //declare the actual struct type mixin StructWithInt!("MyStructA", q{double b;}); //and again for a different struct mixin Prefilled!("StructWithLots", q{float z; char[][] ns; ulong id;}); mixin StructWithLots!("MyStructB", q{void* data;}); void main() { MyStructA foo; foo.a = 2; foo.b = 4.65; MyStructB bar; bar.z = 3.2; //etc..... }
FYI q {} syntax is optional, you can just pass regular strings.
source share