Post-ctor ctor

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Aug 7 21:08:30 PDT 2011


On 8/7/11 1:16 PM, Andrej Mitrovic wrote:
> Just throwing an idea..
>
> structs are great since you don't have to make a special constructor
> just to initialize its fields. E.g.:
>
> struct Foo
> {
>      int a;
>      int b;
>      int c;
>      int d;
> }
>
> void main()
> {
>      auto foo = Foo(1, 2, 3, 4);
> }
>
> But what if I add an extra couple of fields that have to be
> initialized based on only the first 4 fields, and I still want to keep
> the same call in the user code?

Just define a mixin and use it:

this(int a, int b, int c, int d) {
   mixin(initFromParameters());
   ...
}

A similar macro could help assignments.


Andrei


More information about the Digitalmars-d mailing list