Thread overview
I thought mixins didn't override?
Aug 09, 2016
Engine Machine
Aug 09, 2016
Ali Çehreli
Aug 09, 2016
ag0aep6g
August 09, 2016
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.


August 09, 2016
On 08/09/2016 03:10 PM, Engine Machine wrote:
> I try to use a mixin template and redefine some behaviors but D includes
> both and then I get ambiguity.

It's not always possible to understand without seeing actual code.

> 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.

I've come up with the following code from your description. It compiles and prints "from struct" with DMD64 D Compiler v2.071.2-b2:

import std.stdio;

mixin template X() {
    void foo() { writefln("from mixin");}
}

struct s
{
   mixin X;
    void foo() { writefln("from struct"); }
}

void main() {
    auto a = s();
    a.foo();
}

How is your code different?

Ali

August 10, 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