www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Alias lamda argument vs Type template argument

reply Freddy <Hexagonalstar64 gmail.com> writes:
There are two these different ways to pass functions as template 
arguments. Which is preferred?
---
void funcA(alias calle)()
{
     calle();
}

void funcB(T)(T calle)
{
     calle();
}

void main()
{
     funcA!(() => 0);
     funcB(() => 0);
}
---
Oct 15 2015
parent Rikki Cattermole <alphaglosined gmail.com> writes:
On 16/10/15 4:02 PM, Freddy wrote:
 There are two these different ways to pass functions as template
 arguments. Which is preferred?
 ---
 void funcA(alias calle)()
 {
      calle();
 }

 void funcB(T)(T calle)
 {
      calle();
 }

 void main()
 {
      funcA!(() => 0);
      funcB(() => 0);
 }
 ---
Depends, do you need it at compile time or at runtime? funcA is at compile time and funcB is at runtime. If at runtime, you'll probably want to define it anyway and not bother with templates. If you are passing it in for compile time, you are probably doing it for usage with traits. In any case, your better off calling by runtime args. Since you know the arguments and the return type. At least per your example.
Oct 15 2015