www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Mixin template function

reply "cal" <callumenator gmail.com> writes:
Should the following work?

import std.traits;

mixin template Foo()
{
     void foo(T)(T t) if (isSomeString!T) {}
}

class A
{
     void foo()(int i){}
     mixin Foo;
}

void main()
{
     auto a = new A;
     a.foo("hello");
}

Error: template hello.A.foo does not match any function template 
declaration. Candidates are:
hello.A.foo()(int i)

If i give the mixin an identifier (mixin Foo _foo) and call it 
like a._foo.foo("hello") then it works. I thought it should work 
without that though.
Feb 13 2013
next sibling parent reply "cal" <callumenator gmail.com> writes:
And a related question:

class A
{
     void foo(int i){}
     void foo(Tuple!(int) i){}
}

class B: A
{
     override void foo(int i){}
}


int main()
{

     auto b = new B;
     b.foo(tuple(5));
}

This fails to compile. Why can't B use A's tuple overload of 
foo()? If I do this:

class B: A
{
     override void foo(int i){}
     void foo(Tuple!(int) i){} // no override keyword is deprecated
}

The compiler warns about not using the override keyword, so it 
must be seeing the function?
Feb 13 2013
next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Thursday, 14 February 2013 at 05:49:33 UTC, cal wrote:
 And a related question:

 class A
 {
     void foo(int i){}
     void foo(Tuple!(int) i){}
 }

 class B: A
 {
     override void foo(int i){}
 }


 int main()
 {

     auto b = new B;
     b.foo(tuple(5));
 }

 This fails to compile. Why can't B use A's tuple overload of 
 foo()? If I do this:

 class B: A
 {
     override void foo(int i){}
     void foo(Tuple!(int) i){} // no override keyword is 
 deprecated
 }

 The compiler warns about not using the override keyword, so it 
 must be seeing the function?
This looks like it comes from C++, and is a built-in protection. If you override a single method, it will shadow all other overloads. This makes sure you don't accidentally call something you didn't want over-ridden. If you know what you are doing, then you can make it explicit. C++ uses the "using" keyword. I don't know how D does it. http://stackoverflow.com/questions/888235/overriding-a-bases-overloaded-function-in-c
Feb 13 2013
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2013-02-14 06:49, cal wrote:
 And a related question:

 class A
 {
      void foo(int i){}
      void foo(Tuple!(int) i){}
 }

 class B: A
 {
      override void foo(int i){}
 }


 int main()
 {

      auto b = new B;
      b.foo(tuple(5));
 }

 This fails to compile. Why can't B use A's tuple overload of foo()? If I
 do this:

 class B: A
 {
      override void foo(int i){}
      void foo(Tuple!(int) i){} // no override keyword is deprecated
 }

 The compiler warns about not using the override keyword, so it must be
 seeing the function?
The base class and the subclass have different overload sets. You need to bring in "foo" from the base class into the overload set in the subclass. You can do this by using an alias: class B : A { alias A.foo foo; } -- /Jacob Carlborg
Feb 13 2013
prev sibling next sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Thursday, 14 February 2013 at 00:29:51 UTC, cal wrote:
 Should the following work?

 import std.traits;

 mixin template Foo()
 {
     void foo(T)(T t) if (isSomeString!T) {}
 }

 class A
 {
     void foo()(int i){}
     mixin Foo;
 }

 void main()
 {
     auto a = new A;
     a.foo("hello");
 }

 Error: template hello.A.foo does not match any function 
 template declaration. Candidates are:
 hello.A.foo()(int i)

 If i give the mixin an identifier (mixin Foo _foo) and call it 
 like a._foo.foo("hello") then it works. I thought it should 
 work without that though.
AFAIK, this is a bug. It has *probably* already been reported, but you'd have to check for it.
Feb 13 2013
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2013-02-14 01:29, cal wrote:
 Should the following work?

 import std.traits;

 mixin template Foo()
 {
      void foo(T)(T t) if (isSomeString!T) {}
 }

 class A
 {
      void foo()(int i){}
      mixin Foo;
 }

 void main()
 {
      auto a = new A;
      a.foo("hello");
 }

 Error: template hello.A.foo does not match any function template
 declaration. Candidates are:
 hello.A.foo()(int i)

 If i give the mixin an identifier (mixin Foo _foo) and call it like
 a._foo.foo("hello") then it works. I thought it should work without that
 though.
This is by design. Foo and A have different overload sets. Try: alias Foo.foo foo; http://dlang.org/template-mixin.html Search for: "Mixin Scope" and pay attention to: "Alias declarations can be used to overload together functions declared in different mixins". -- /Jacob Carlborg
Feb 13 2013
parent "cal" <callumenator gmail.com> writes:
On Thursday, 14 February 2013 at 07:40:58 UTC, Jacob Carlborg 
wrote:
 This is by design. Foo and A have different overload sets. Try:

 alias Foo.foo foo;

 http://dlang.org/template-mixin.html

 Search for: "Mixin Scope" and pay attention to:

 "Alias declarations can be used to overload together functions 
 declared in different mixins".
Ahh this is what I was missing, many thanks.
Feb 13 2013