On 10/29/2011 05:24 PM, J Arrizza wrote:
I have a template that I'd like to have a specialization for arrays.
Initiall I need it to work for byte arrays, but I'd like to make it
eventually work for all arrays. The page
http://d-programming-language.org/template says to use

    template TFoo(T : T[]) { ... } // #2


but when I try it, it doesn't quite work:

    template abc(T)
       {
         void abc(T parm1)
           {
             writeln("simpleparm: ", parm1);
           }
       }

    template abc(T: T[])
       {
         void abc(T parm1)
           {
             writeln("array : ", parm1);
           }
       }


    void main(string[] args)
       {
         abc(1);
         abc("str");
         int[] arr = [1, 2];
         abc(arr);
       }


The output is:

    simpleparm: 1
    simpleparm: str
    simpleparm: [1, 2]


Which is not what I want, it needs to be the specialized  template for
arrays. Note, this doesn't work either:

    template abc(T: T[])
       {
         void abc(T[] parm1)
           {
             writeln("array : ", parm1);
           }
       }


John

Fixed:

template abc(T) {
    void abc(T parm1) {
        writeln("simpleparm: ", parm1);
    }
}

void abc(T:T[])(T[] parm1) {
    writeln("array : ", parm1);
}


void main(string[] args) {
    abc(1);
    abc!(typeof("str"))("str");
    int[] arr = [1, 2];
    abc!(int[])(arr);
}


The important thing to note is that when pattern matching on T[] is done, then T is the element type of the array, not the array type.


Reply via email to