template fiddling

Mafi mafi at example.org
Sat Aug 7 13:05:35 PDT 2010


Am 07.08.2010 21:04, schrieb Blonder:
> Hello,
> the template!(...) mechanism I understand.
> But I think I need the two classes, the first is the primary template
> and the second the partial specialization (this is the end of the "loop"), or can
> I do this in D with functions?
>
> The normal computation of the dot product is normally done in a for loop. But with
> templates you can enroll the loop.
> template!(...)(3, a, b). In this example 3 is the dimension of the arrays a and b.
>
> Andreas.
Is that what you are searching for:
/////////////
module templatetest;

import std.stdio;

T[] foo(T : T[], int S) (T[] t) {
   writeln("Hey ",S);
   return foo!(T[],S-1)(t);
}

T[] foo(T : T[], int S : 0) (T[] t) {
   writeln("END");
   return(t);
}

/*
//Can't get static arrays right
//always uses dynamic version
T[S] foo(T : T[S], int S) (T[S] t) {
   writeln("Hello ",S);
   return foo!(T[],S-1)(t);
}

T[S] foo(T : T[S], int S:0) (T[S] t) {
   writeln("END2");
}*/

void main() {
   int[] x = [25,42,26,34,10];
   foo!(int[],5)(x);
}
//////////////
Uage foo!(int[],5)(xy). If you swap around the array parameters in your 
version then you should be able to omit T.
Also when you have fixed-size arrays the array length isn't DRY.


More information about the Digitalmars-d-learn mailing list