Nicer syntax for constructors

Jacob Carlborg doob at me.com
Sun Nov 18 19:47:26 UTC 2018


On 2018-11-18 20:26, Trailzz wrote:
> On Sunday, 18 November 2018 at 19:25:09 UTC, 12345swordy wrote:
>> Have tried to use templates/string mixin for this? Don't rush to a DIP 
>> yet, until you exhaust other options.
> 
> how would you use templates/string mixin for this?

Something like this:

import std.traits : ParameterIdentifierTuple;

class Foo
{
     int a;
     int b;

     this(int a, int b)
     {
         static foreach (name ; ParameterIdentifierTuple!(__ctor))
             __traits(getMember, this, name) = mixin(name);
     }
}

void main()
{
     auto f = new Foo(2, 3);
     assert(f.a == 2);
     assert(f.b == 3);
}

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list