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

Robert M. Münch robert.muench at saphirion.com
Fri Jun 29 21:40:37 UTC 2018


How does it work if one of the members takes an argument that is 
deduced inside the handler function?


On 2018-06-29 18:05:00 +0000, Ali ‡ehreli said:

> Passing a lambda or a string mixin:
> 
> import std.stdio;
> 
> class C {
>      void A() {
>          writeln(__FUNCTION__);
>      }
> 
>      void B() {
>          writeln(__FUNCTION__);
>      }

void C(bool flag) {
	writln(__FUNCTION__):
}

> }
> 
> void handler(alias func)(C[] cs) {
>      foreach (c; cs) {
>          func(c);

func(c)(flag);

>      }
> }
> 
> void handler_2(string func)(C[] cs) {
>      foreach (c; cs) {
>          enum expr = "c." ~ func ~ "();";
>          mixin(expr);
>      }
> }
> 
> void main() {
>      auto cs = [ new C(), new C() ];
> 
>      handler!(o => o.A())(cs);

How to get the parameter spec into the lambda?

>      handler!(o => o.B())(cs);
> 
>      handler_2!"A"(cs);
>      handler_2!"B"(cs);
> }
> 
> Ali


-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



More information about the Digitalmars-d-learn mailing list