Method overloading without Type2Type

Philippe Sigaud philippe.sigaud at gmail.com
Tue Apr 26 12:55:22 PDT 2011


On Tue, Apr 26, 2011 at 18:41, Benjamin Thaut <code at benjamin-thaut.de> wrote:
> Thanks, but that is not connected to my question at all,
> I want to implement the echo method so that the type is passed as a template
> argument, and not as a function argument. While that is happening I still
> want to be able to overload the function.
> Is this possible in D 2?

I'm not sure I understand what you're trying to do (didn't read
"Modern C++ design"). Here is something that compiles:

import std.stdio;

class Foo(T,R...) : Foo!(R) {
       public void print(){
               writeln(T.stringof);
               super.print();
       }

       public void echo(U)(U u = U.init) {
               writeln(U.stringof);
       }
}

class Foo(T){
       public void print(){
               writeln("end: " ~ T.stringof);
       }

       public void echo(U)(U u = U.init) {
               writeln(U.stringof);
       }
}

void main(string[] args){
       auto test = new Foo!(int,float,double,short,byte)();
       test.print();

       test.echo!double;
       test.echo!short;
}


I don't know if classes are necessary for what you've in mind. I tend
to use structs:

import std.stdio;

struct Foo(T,R...) {
    void print(){
        static if (R.length)
        {
            Foo!(R) fr;
            fr.print;
        }
        writeln(T.stringof);
    }

    void echo(U)(U u = U.init) { writefln(U.stringof);}
}

void main(string[] args){
       Foo!(int,float,double,short,byte) test;
       test.print;

       test.echo!double;
       test.echo!short;
}


More information about the Digitalmars-d-learn mailing list