Nicer syntax for constructors

Trailzz email at example.com
Sun Nov 18 20:06:14 UTC 2018


On Sunday, 18 November 2018 at 19:59:27 UTC, Timon Gehr wrote:
> On 18.11.18 20:52, Trailzz wrote:
>> On Sunday, 18 November 2018 at 19:47:26 UTC, Jacob Carlborg 
>> wrote:
>>> [...]
>> 
>> 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));
> }
> ---

This is arguably more clunky than just lots of `this.foo = foo`.


More information about the Digitalmars-d mailing list