www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - mixin template and overloading

reply "Luc Bourhis" <ljbo nowhere.com> writes:
Consider:

~ % dmd -v|head -n 1
DMD64 D Compiler v2.066-devel

~% cat mixin_template_pb.d
mixin template Foo(T) {
   void bar() {}
}

struct FooBar {
   mixin Foo!int;
   void bar(ulong d)() {}
}

void check() {
   FooBar o;
   o.bar();
}
~% dmd -c mixin_template_pb.d
mixin_template_pb.d(12): Error: template 
mixin_template_pb.FooBar.bar cannot deduce function from argument 
types !()(), candidates are:
mixin_template_pb.d(7):        mixin_template_pb.FooBar.bar(ulong 
d)()

It looks like the compiler does not see the mixed-in "bar". If I 
comment out the definition of "bar" in "FooBar", it compiles 
fine. Is this to be considered a bug?
Jan 20 2015
parent "anonymous" <anonymous example.com> writes:
On Tuesday, 20 January 2015 at 11:30:39 UTC, Luc Bourhis wrote:
 Consider:

 ~ % dmd -v|head -n 1
 DMD64 D Compiler v2.066-devel

 ~% cat mixin_template_pb.d
 mixin template Foo(T) {
   void bar() {}
 }

 struct FooBar {
   mixin Foo!int;
   void bar(ulong d)() {}
 }

 void check() {
   FooBar o;
   o.bar();
 }
 ~% dmd -c mixin_template_pb.d
 mixin_template_pb.d(12): Error: template 
 mixin_template_pb.FooBar.bar cannot deduce function from 
 argument types !()(), candidates are:
 mixin_template_pb.d(7):        
 mixin_template_pb.FooBar.bar(ulong d)()

 It looks like the compiler does not see the mixed-in "bar". If 
 I comment out the definition of "bar" in "FooBar", it compiles 
 fine. Is this to be considered a bug?
No, it's working as intended: "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" -- http://dlang.org/template-mixin.html You can use `alias` to bring the two together: struct FooBar { mixin Foo!int f; alias bar = f.bar; void bar(ulong d)() {} }
Jan 20 2015