Nicer syntax for constructors

FeepingCreature feepingcreature at gmail.de
Mon Nov 19 00:18:52 UTC 2018


On Sunday, 18 November 2018 at 19:09:16 UTC, Trailzz wrote:
> This situation happens *very* often in object oriented 
> programming:
> ```
> this(string a, int b, char c, float d, uint e, bool f)
> {
>     this.a = a;
>     this.b = b;
>     this.c = c;
>     this.d = d;
>     this.e = e;
>     this.f = f;
> }
> ```
>
> It would save a lot of code bloat if there was a simpler syntax 
> for this, perhaps something like this:
> ```
> this(string this.a, int this.b, char this.c, float this.d, uint 
> this.e, bool this.f)
> {
> }
> ```
>
> It would be very easy to infer the types here, so this could 
> also work
> ```
> this(this.a, this.b, this.c, this.d, this.e, this.f)
> {
> }
> ```
>
> I'm not sure exactly what the syntax would be like, but I just 
> wanted to know what other people think about this. Would it be 
> worth creating a DIP for this?

You can use my boilerplate library 
https://code.dlang.org/packages/boilerplate to automatically 
generate constructors for pod classes. Just mixin(GenerateThis);


More information about the Digitalmars-d mailing list