Thread overview | |||||
---|---|---|---|---|---|
|
September 13, 2020 Why does a directly defined constructor hide a mixed-in constructor? | ||||
---|---|---|---|---|
| ||||
This code: --- mixin template X() { int[2] x; this(int[2] x...) { this.x = x; } } struct Foo { } struct Bar { mixin X; this(Foo foo) { this.x = [0, 0]; } } void main() { auto bar = Bar(1, 2); } --- produces the following error: --- source/app.d(27,17): Error: constructor app.Bar.this(Foo foo) is not callable using argument types (int, int) source/app.d(27,17): cannot pass argument 1 of type int to parameter Foo foo --- However, if I directly insert the contents of X into Bar instead of mixing it in, it compiles just fine. What's going on here? |
September 13, 2020 Re: Why does a directly defined constructor hide a mixed-in constructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to 60rntogo | On Sunday, 13 September 2020 at 12:34:06 UTC, 60rntogo wrote: > However, if I directly insert the contents of X into Bar instead of mixing it in, it compiles just fine. What's going on here? You can override members from mixin templates by giving a member with the same *name* (not the same signature!) directly. mixin template foo() { int a; } class Thing { mixin foo; string a; /* this a overrides foo's a */ } This is pretty useful in a lot of cases but kinda annoying with overloading. To overload, you must use `alias` to merge the overload sets. For constructors, you need to use the name `__ctor` instead of `this` to make it compile: ``` struct Bar { mixin X some_name; // notice the addition of a name this(Foo foo) { this.x = [0, 0]; } alias __ctor = some_name.__ctor; // merge the overloads } ``` Read more here: http://dpldocs.info/this-week-in-d/Blog.Posted_2020_01_20.html#understanding-mixin-templates and here too: https://stackoverflow.com/a/57712459/1457000 |
September 13, 2020 Re: Why does a directly defined constructor hide a mixed-in constructor? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, 13 September 2020 at 13:10:15 UTC, Adam D. Ruppe wrote:
> This is pretty useful in a lot of cases but kinda annoying with overloading. To overload, you must use `alias` to merge the overload sets. For constructors, you need to use the name `__ctor` instead of `this` to make it compile:
Yes, that works. Thanks!
|
Copyright © 1999-2021 by the D Language Foundation