Why does a directly defined constructor hide a mixed-in constructor?

Adam D. Ruppe destructionator at gmail.com
Sun Sep 13 13:10:15 UTC 2020


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


More information about the Digitalmars-d-learn mailing list