Template mixins and struct constructors

Adrian Matoga via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 2 05:18:23 PST 2016


On Wednesday, 2 March 2016 at 12:48:47 UTC, Daniel Kozak wrote:
> On Wednesday, 2 March 2016 at 12:27:04 UTC, Adrian Matoga wrote:
>> (...)
>
> You can use string mixins:
>
> template AddField(T) {
>         enum AddField = T.stringof ~ `  b;
>         this(Args...)(` ~ T.stringof ~ ` b, auto ref Args args)
>         {
>                 this.b = b;
>                 this(args);
>         }`;
> }
>
> struct Bar {
>         mixin(AddField!string);
>         int a;
>         this(int a) { this.a = a; }
> }
>
> unittest {
>         auto bar1 = Bar(5);
>         auto bar2 = Bar("bar", 15);  // line 31
> }

What if T is private type in some other module or, even worse, a 
Voldemort type?


More information about the Digitalmars-d-learn mailing list