www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Templates and Functions

reply Oliver <ruebenko imtek.de> writes:
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
parent reply Regan Heath <regan netmail.co.nz> writes:
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
next sibling parent Regan Heath <regan netmail.co.nz> writes:
Regan Heath wrote:
 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
If you also call it with: auto d = dMap!(real,double[])(fp,b); it works, but that's a pain. Regan
Sep 12 2007
prev sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
parent reply Regan Heath <regan netmail.co.nz> writes:
Jarrett Billingsley wrote:
 "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.
I would really like to know 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
parent oliver <ruebenko imtek.de> writes:
Regan Heath Wrote:

 I would really like to know 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
thanks, i had exactly the same problem - could have sworn i tried that ;-) oliver
Sep 12 2007