template mixins for boilerplate

Paul D Anderson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 20 20:32:01 PDT 2014


I am misunderstanding something about using mixins for 
boilerplate code.

I've got a set of functions all of which do the same thing:

public static int fctn1() { return other.place.fctn1; }

I can't use a string mixin to generate the code:

template Function(string name)
{
   const char[] Function =
     "public static int " ~ name ~ "() { return other.place." ~ 
name ~"; }";
}

struct S {
mixin (Function!("fctn1"));
}

Error: "cannot use a template to add field to aggregate S".

I can't use a template mixin:

mixin template Function(string name)
{
   const char[] Function =
     "public static int " ~ name ~ "() { return other.module." ~ 
name ~"; }";
}

Error: mixin templates are not regular templates.

Is there a way to duplicate fctn1 with a different name each time?

Paul


More information about the Digitalmars-d-learn mailing list