Call different member functions on object sequence with a generic handler function?
Robert M. Münch
robert.muench at saphirion.com
Fri Jun 29 20:08:56 UTC 2018
On 2018-06-29 18:05:00 +0000, Ali ehreli said:
> On 06/29/2018 09:44 AM, Robert M. Münch wrote:
>
>> So, how can I write a generic handler that does the iteration, where I
>> can specify which member function to call?
>
> Passing a lambda or a string mixin:
Hi, that was somehow in my mind but didn't made to get the syntax
clear... Thanks a lot.
> import std.stdio;
>
> class C {
> void A() {
> writeln(__FUNCTION__);
> }
>
> void B() {
> writeln(__FUNCTION__);
> }
> }
>
> 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?
> void handler_2(string func)(C[] cs) {
> foreach (c; cs) {
> enum expr = "c." ~ func ~ "();";
> mixin(expr);
> }
> }
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
--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster
More information about the Digitalmars-d-learn
mailing list