Complex Object Generation with Templates/Mixins

Chris Williams littleratblue at yahoo.co.jp
Tue Jun 2 11:01:41 PDT 2009


Jarrett Billingsley Wrote:
> You'll be happy to know that your code compiles and works with very
> little modification.
> 
> char[] ClassGen(char[] className, char[][][] classMembers) {
>   char[] ret = "class " ~ className ~ " {";
>   foreach(member; classMembers)
>      ret ~= member[0] ~ " " ~ member[1] ~ ";";
> 
>   return ret ~ "}";
> }
> 
> mixin(
>   ClassGen(
>      "Foo",
>      [
>         ["int", "bar"],
>         ["int", "baz"]
>      ]
>   )
> );
> 
> You can't use for loops in templates, so instead you have to use
> recursion.  In this case, however, a CTFE function is much terser.

Wonderful. Thank you.



More information about the Digitalmars-d mailing list