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:


and here too:


More information about the Digitalmars-d-learn mailing list