Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
September 12, 2007 Templates and Functions | ||||
---|---|---|---|---|
| ||||
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); } |
September 12, 2007 Re: Templates and Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Oliver | 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
|
September 12, 2007 Re: Templates and Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | 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
|
September 12, 2007 Re: Templates and Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | "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. :) |
September 12, 2007 Re: Templates and Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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 |
September 12, 2007 Re: Templates and Functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | 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
|
Copyright © 1999-2021 by the D Language Foundation