www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Template method overloading

reply Dominic Letz <dominic.letz berlin.de> writes:
Hello, 

I'm trying to overload a template method. But it seems not to produce the
expected result, although everything is compiling fine.

My sample is this: (Compiled with win32 dmd version 1.0). I expect:
A is called with 1
B is called with 2
But I got this:
A is called with 1
A is called with 2

the file:
import std.stdio;
class A
{
    void someMethod(T)(T i)
    {
        writefln("A is called with %d", i);
    }
}
class B : A
{
    void someMethod(T)(T i)
    {
        writefln("B is called with %d", i);
    }
}
int main()
{
    A test = new A();
    test.someMethod(1);
    test = new B();
    test.someMethod(2); 
    return 0;
}
Jan 23 2007
parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
Dominic Letz wrote:
 Hello, 
 
 I'm trying to overload a template method. But it seems not to produce the
expected result, although everything is compiling fine.
 
 My sample is this: (Compiled with win32 dmd version 1.0). I expect:
 A is called with 1
 B is called with 2
 But I got this:
 A is called with 1
 A is called with 2
 
 the file:
 import std.stdio;
 class A
 {
     void someMethod(T)(T i)
     {
         writefln("A is called with %d", i);
     }
 }
 class B : A
 {
     void someMethod(T)(T i)
     {
         writefln("B is called with %d", i);
     }
 }
 int main()
 {
     A test = new A();
     test.someMethod(1);
     test = new B();
     test.someMethod(2); 
     return 0;
 }
 
Template member functions are implicitly 'final.' This is a consequence of how templates work. They cannot exist in the vtable. -- Kirk McDonald Pyd: Wrapping Python with D http://pyd.dsource.org
Jan 23 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Kirk McDonald wrote:
 Template member functions are implicitly 'final.' This is a consequence 
 of how templates work. They cannot exist in the vtable.
It's a consequence of how templates work, but in combination with how the vtable works. I could imagine a hash-table being added for member templates, which would then be filled at startup time[1]... [1] Current compiler & (especially) linker technology don't allow this to be done at compile or link time. A linear table could be done at link time in a special section, but that might entail too big a performance penalty for lookup :(. Note: No, this was not a particularly serious suggestion :P.
Jan 23 2007