mixed in struct constructor is ignored when another (non mixed in) constructor is specified
ParticlePeter
ParticlePeter at gmx.de
Mon Feb 26 13:09:40 UTC 2018
On Monday, 26 February 2018 at 12:47:48 UTC, Jonathan M Davis
wrote:
> 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
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.
More information about the Digitalmars-d-learn
mailing list