shorter alternative of constructor with parameter

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 23 11:23:55 PDT 2014


On Mon, 23 Jun 2014 14:20:32 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> Gary Willoughby:
>
>> As another dev said a template or mixin can be used for this.
>
> I don't think a template/mixin can replace that sufficiently well,  
> because you also often want to put some code inside the constructor.

The mixin does not have to generate a whole function, it can just generate  
the assigns inside a ctor.

I'm envisioning something like this:

class C
{
    int a;
    int b;
    int c;
    this(int a, int b, int c)
    {
       mixin(ctorAssigns); // this.a = a; this.b = b; this.c = c;

       // other code
    }
}

-Steve


More information about the Digitalmars-d mailing list