digitalmars.D.learn - Templates and Functions
- Oliver (21/21) Sep 12 2007 Hi,
- Regan Heath (8/35) Sep 12 2007 I figured you'd use this:
- Regan Heath (5/50) Sep 12 2007 If you also call it with:
- Jarrett Billingsley (6/11) Sep 12 2007 Specialization + IFTI = error, I'm not entirely sure why. But
- Regan Heath (4/22) Sep 12 2007 I could have sworn I tried that.
- oliver (3/15) Sep 12 2007 thanks, i had exactly the same problem - could have sworn i tried that ;...
Hi, I'd like to hand over a function to a template. However I am unable to get access to the functions argument type. In the following code the function's argument has been set to double but it should actually be of type T2. T2 is this case is an array and i would like something to give me the type of the array which i can then use to replace the double with. Any suggestions on how to do this? Thanks a lot. ------- import std.stdio; import std.math; T1 [] dMap( T1, T2 )( T1 function(double) fp, T2 array) { T1 newArray[]; newArray.length = array.length; for(int i = 0; i < array.length; i++) { newArray[i] = fp(array[i]); }; return newArray; } void main() { auto b = [0., 6.5, 3.1415]; real function(double a) fp; fp = function(double a) { return sin(a);} ; auto d = dMap(fp,b); writefln("d = ", d); }
Sep 12 2007
Oliver wrote:Hi, I'd like to hand over a function to a template. However I am unable to get access to the functions argument type. In the following code the function's argument has been set to double but it should actually be of type T2. T2 is this case is an array and i would like something to give me the type of the array which i can then use to replace the double with. Any suggestions on how to do this? Thanks a lot. ------- import std.stdio; import std.math; T1 [] dMap( T1, T2 )( T1 function(double) fp, T2 array) { T1 newArray[]; newArray.length = array.length; for(int i = 0; i < array.length; i++) { newArray[i] = fp(array[i]); }; return newArray; } void main() { auto b = [0., 6.5, 3.1415]; real function(double a) fp; fp = function(double a) { return sin(a);} ; auto d = dMap(fp,b); writefln("d = ", d); }I figured you'd use this: T1 [] dMap( T1, T2:T2[] )( T1 function(T2) fp, T2[] array) { But that gives: template dbl.dMap(T1,T2 : T2[]) specialization not allowed for deduced parameter T2 and I have no idea why specialization is not allowed :( Regan
Sep 12 2007
Regan Heath wrote:Oliver wrote:If you also call it with: auto d = dMap!(real,double[])(fp,b); it works, but that's a pain. ReganHi, I'd like to hand over a function to a template. However I am unable to get access to the functions argument type. In the following code the function's argument has been set to double but it should actually be of type T2. T2 is this case is an array and i would like something to give me the type of the array which i can then use to replace the double with. Any suggestions on how to do this? Thanks a lot. ------- import std.stdio; import std.math; T1 [] dMap( T1, T2 )( T1 function(double) fp, T2 array) { T1 newArray[]; newArray.length = array.length; for(int i = 0; i < array.length; i++) { newArray[i] = fp(array[i]); }; return newArray; } void main() { auto b = [0., 6.5, 3.1415]; real function(double a) fp; fp = function(double a) { return sin(a);} ; auto d = dMap(fp,b); writefln("d = ", d); }I figured you'd use this: T1 [] dMap( T1, T2:T2[] )( T1 function(T2) fp, T2[] array) { But that gives: template dbl.dMap(T1,T2 : T2[]) specialization not allowed for deduced parameter T2 and I have no idea why specialization is not allowed :( Regan
Sep 12 2007
"Regan Heath" <regan netmail.co.nz> wrote in message news:fc8giq$12m$1 digitalmars.com...T1 [] dMap( T1, T2:T2[] )( T1 function(T2) fp, T2[] array) { But that gives: template dbl.dMap(T1,T2 : T2[]) specialization not allowed for deduced parameter T2 and I have no idea why specialization is not allowed :(Specialization + IFTI = error, I'm not entirely sure why. But specialization isn't needed here: T1 [] dMap( T1, T2 )( T1 function(T2) fp, T2[] array) { works fine. :)
Sep 12 2007
Jarrett Billingsley wrote:"Regan Heath" <regan netmail.co.nz> wrote in message news:fc8giq$12m$1 digitalmars.com...I would really like to know why.T1 [] dMap( T1, T2:T2[] )( T1 function(T2) fp, T2[] array) { But that gives: template dbl.dMap(T1,T2 : T2[]) specialization not allowed for deduced parameter T2 and I have no idea why specialization is not allowed :(Specialization + IFTI = error, I'm not entirely sure why.But specialization isn't needed here: T1 [] dMap( T1, T2 )( T1 function(T2) fp, T2[] array) { works fine. :)I could have sworn I tried that. Regan
Sep 12 2007
Regan Heath Wrote:I would really like to know why. > Butthanks, i had exactly the same problem - could have sworn i tried that ;-) oliverspecialization isn't needed here: T1 [] dMap( T1, T2 )( T1 function(T2) fp, T2[] array) { works fine. :)I could have sworn I tried that. Regan
Sep 12 2007