Complex Object Generation with Templates/Mixins

Chris Williams littleratblue at yahoo.co.jp
Tue Jun 2 09:03:27 PDT 2009


I would like to do something similar to the example at:

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

I want to create a class dynamically, but I want to be able to add an unspecified number of items to it. Pseudo-code, but something like this:

template ClassGen(char[] className, char[][][] classMembers) {
   const char[] ClassGen = "class " ~ className ~ " {";
   for (int i = 0; i < classMembers.length; i++) {
      classGen ~= classMembers[i][0] ~ " " ~ classMembers[i][1] ~ ";"
   }
   classGen ~= "}";
}

mixin(
   ClassGen!(
      "Foo",
      [
         ["int", "bar"],
         ["int", "baz"]
      ]
   )
);

It seems like somehow between templates, mixins, and tuples (tuple arrays?) that something like this should be possible, but I'm still figuring out the language.



More information about the Digitalmars-d mailing list