I thought mixins didn't override?

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 9 15:20:01 PDT 2016


On 08/10/2016 12:10 AM, Engine Machine wrote:
> I try to use a mixin template and redefine some behaviors but D includes
> both and then I get ambiguity. I was sure I read somewhere that when one
> uses mixin template it won't include what is already there?
>
> mixin template X
> {
>    void foo() { }
> }
>
> struct s
> {
>    mixin template
>    void foo() { }
> }
>
> I was pretty sure I read somewhere that D would not include the foo from
> the template since it already exists.

Please post proper code.

This compiles and calls the foo that's not being mixed in:

----
import std.stdio;

mixin template X()
{
    void foo() { writeln("mixed in"); }
}

struct s
{
    mixin X;
    void foo() { writeln("not mixed in"); }
}

void main()
{
     s obj;
     obj.foo();
}
----

This is in line with the spec, which says: "If the name of a declaration 
in a mixin is the same as a declaration in the surrounding scope, the 
surrounding declaration overrides the mixin one" [1]. That may be what 
you've read.


[1] http://dlang.org/spec/template-mixin.html#mixin_scope


More information about the Digitalmars-d-learn mailing list