mixed in struct constructor is ignored when another (non mixed in) constructor is specified

Simen Kjærås simen.kjaras at gmail.com
Mon Feb 26 13:35:33 UTC 2018


On Monday, 26 February 2018 at 13:09:40 UTC, ParticlePeter wrote:
> Thanks for clarification, unfortunately your suggestion doesn't 
> work.
> Since when is alias this = something; supposed to work?
> alias Common.this this; doesn't work as well and following also 
> not:
>
> struct Baz {
>   Foo foo;
>   alias foo this;
>   this( int m, int n ) { m_member = m * n; }
> }
>
> auto baz = Baz(1); // Error, same as above
>
> Not sure if I do require an enhancement, I just stumbled on 
> that and was wondering.

The workaround for now is to define your other constructor in 
another mixin template:

struct Bar {
     mixin Common;

     mixin template Common2() {this( int m, float n ) { m_member = 
cast(int)(m * n); } };
     mixin Common2;
}

It's not exactly pretty, but it works.

An issue has already been filed for this: 
https://issues.dlang.org/show_bug.cgi?id=17055.

--
   Simen


More information about the Digitalmars-d-learn mailing list