Call different member functions on object sequence with a generic handler function?

Timoses timosesu at gmail.com
Fri Jun 29 20:23:47 UTC 2018


On Friday, 29 June 2018 at 20:08:56 UTC, Robert M. Münch wrote:
> On 2018-06-29 18:05:00 +0000, Ali ‡ehreli said:
>
>> On 06/29/2018 09:44 AM, Robert M. Münch wrote:
>> void handler(alias func)(C[] cs) {
>>      foreach (c; cs) {
>>          func(c);
>>      }
>> }
>
> Is it possible to make C[] a template type so that I can use 
> different classes and lambdas?

My guess would be:

void handler(alias func, T)(T[] ts) {
     ....
}

>
> Ok, the "strange" syntax for me is the handler(alias func) or 
> handler(string func) ... I'm alway thinkin in lines of a 
> parameter... which of course doesn't work.
>
> Looking at the D grammer for functions these things should be 
> the FuncDeclaratorSuffix, is this right? And then, which part 
> does this fall into?
>
> FuncDeclaratorSuffix:
>    Parameters MemberFunctionAttributesopt
>    TemplateParameters Parameters MemberFunctionAttributesop

It's definitely the "TemplateParameters" one:

void func(<template params>)(<params>) { ... }

is the short form of

template(<template params>)
{
     void func(<params>) { ... }
}

https://dlang.org/spec/template.html


More information about the Digitalmars-d-learn mailing list