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

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Feb 26 12:47:48 UTC 2018


On Monday, February 26, 2018 12:30:24 ParticlePeter via Digitalmars-d-learn 
wrote:
> mixin template Common() {
>    private int m_member;
>    this( int m ) { m_member = m; }
> }
>
> struct Foo {
>    mixin Common;
> }
>
> struct Bar {
>    mixin Common;
>    this( int m, float n ) { m_member = m * n; }
> }
>
>
> auto foo = Foo(1);       // ok
> auto b_1 = Bar( 1, 2 );  // ok
> auto b_2 = Bar( 3 );     // Error: constructor main.Bar.this (int
> m, int n) is not callable using argument types (int)
>
> Is this expected behavior?

Yes. Stuff that's mixed in is treated as having a different scope than other
mixins or stuff that wasn't mixed in. To quote towards the bottom of here:

https://dlang.org/spec/template-mixin.html

"Alias declarations can be used to overload together functions declared in
different mixins"

It gives an example of

mixin Foo!() F;
mixin Bar!() B;

alias func = F.func;
alias func = B.func;

I don't know if you can do the same thing with constructors or not, though
alias this statements don't allow the = syntax. So, maybe that won't
conflict, and it will work to do something like

alias this = Common.this;

If that doesn't work, then it seems like a good enhancement request.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list