Mixin a constructor ?

Christopher Wright dhasenan at gmail.com
Sat Sep 19 11:55:57 PDT 2009


Jacob Carlborg wrote:
> Is it supposed to possible to mixin a constructor? The code below 
> doesn't compile. The error: is "main.d(23): Error: constructor 
> main.A.this() does not match parameter types (int)
> main.d(23): Error: expected 0 arguments, not 1"

A template mixin introduces a new scope. This is pretty annoying in some 
cases. More often, it is convenient. Since the constructor is not in the 
same scope as the class, it might be causing problems.

If you didn't introduce a new scope by default, it'd be easy enough to 
add it in the template or around the mixin:

template MixMeIn()
{
    int imInUrScope;
    {
       int imOuttaUrScope;
    }
}


Additionally, the difference would be easily detectable, because symbol 
collision would cause the code to fail to compile. Unless the mixin adds 
a function overload and you pass the address of the overload set 
somewhere, and now you're passing the wrong overload...which is an old 
problem for D, aggravated by D's property syntax, and unlikely to be 
fixed soon.



More information about the Digitalmars-d mailing list