Nicer syntax for constructors

Timon Gehr timon.gehr at gmx.ch
Sun Nov 18 19:59:27 UTC 2018


On 18.11.18 20:52, Trailzz wrote:
> On Sunday, 18 November 2018 at 19:47:26 UTC, Jacob Carlborg wrote:
>>     this(int a, int b)
>>     {
>>         static foreach (name ; ParameterIdentifierTuple!(__ctor))
>>             __traits(getMember, this, name) = mixin(name);
>>     }
>> }
> 
> This is still very clunky and hard to understand.

---
module util;
enum assignFields=q{{
     import std.traits;
     static foreach(x;ParameterIdentifierTuple!(__traits(parent,{})))
         static if(__traits(hasMember, this, x))
             __traits(getMember,this,x)=mixin(x);
}};
---
---
module app;
import util;

class C{
     string a;
     int b;
     char c;
     float d;
     uint e;
     bool f;
     this(string a, int b, char c, float d, uint e, bool f){
         mixin(assignFields);
     }
}

void main(){
     import std.stdio;
     writeln((new C("1",2,'3',4.0,5,true).tupleof));
}
---


More information about the Digitalmars-d mailing list