www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - aliasing functions with function arguments as well ??

reply james.p.leblanc <james.p.leblanc gmail.com> writes:
Dear All,

How does one use 'alias' to incorporate function arguments as 
well?

(I believe this is possible, from some of the examples of 
aliasSeq, and
the traits.Parameters documentation.  However, I was unable to 
come up
with anything that works.)

What should replace the question marks (???) below?

double bar( int a, double x){
   return a*x:
}
template foo(T){
   static if ( is(T==int) ){
      alias ??? = ???
   }
   else{
      alias ??? = ???
   }
}

// when T == int, I desire the aliasing to produce resulting 
code:
foo( int a) = bar( int a, 42.33);

// when T == double, I desire:
foo( double x) = bar( 7, x);
Thanks kindly for any information! James PS I am aware of the "struct holding a function allowing multiple dispatch concept" ... while that would fit quite okay here in my simple example, it isn't appropriate for my real use.
Aug 13 2021
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/13/21 11:04 AM, james.p.leblanc wrote:
 Dear All,
 
 How does one use 'alias' to incorporate function arguments as well?
 
 (I believe this is possible, from some of the examples of aliasSeq, and
 the traits.Parameters documentation.  However, I was unable to come up
 with anything that works.)
 
 What should replace the question marks (???) below?
 
 double bar( int a, double x){
   return a*x:
 }
 template foo(T){
   static if ( is(T==int) ){
      alias ??? = ???
   }
   else{
      alias ??? = ???
   }
 }

 // when T == int, I desire the aliasing to produce resulting code:
 foo( int a) = bar( int a, 42.33);

 // when T == double, I desire:
 foo( double x) = bar( 7, x);
Thanks kindly for any information! James PS I am aware of the "struct holding a function allowing multiple dispatch concept" ... while that would fit quite okay here in my simple example, it isn't appropriate for my real use.
There isn't a way to alias it. You can wrap it though, and hope the inliner takes care of the difference: ```d auto foo(T)(T arg) { static if(is(T == int)) return bar(arg, 42.33); else return bar(7, arg); } ``` -Steve
Aug 13 2021
parent james.p.leblanc <james.p.leblanc gmail.com> writes:
On Friday, 13 August 2021 at 15:14:00 UTC, Steven Schveighoffer 
wrote:

 There isn't a way to alias it. You can wrap it though, and hope 
 the inliner takes care of the difference:

 ```d
 auto foo(T)(T arg)
 {
    static if(is(T == int)) return bar(arg, 42.33);
    else return bar(7, arg);
 }
 ```

 -Steve
Steve, Thanks! Yes ... this templated wrapper should be perfect for me! Your help on my present question, as well as your continual contributions to the forum are greatly appreciated! Best Regards, James
Aug 13 2021