Code example for function/delegate as template argument ?

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 4 04:37:02 PDT 2016


On Wednesday, 4 May 2016 at 11:19:59 UTC, chmike wrote:
> I think you misunderstood the second question.
>
> Here is another attempt with an example.
>
> // function accepting a function as argument
> void foo(function void fg(int)) {
>     fg(5);
> }
>
> // A class with a none static method with the same signature as 
> the argument function of foo
> class Bar {
>     void fizz(int a) { writefln("Arg: %s", a); }
> }
>
> // An instance of class Bar
> auto bar = new Bar;
>
> // Calling foo by passing bar and the method fizz so that 
> bar.fizz() is called when foo calls fg
> foo( ??? );
>
> Does the argument type need to be a delegate ?

It's much more simple then. You need

void foo(void delegate int() dg) {dg(5);}

and yes it must be a delegate.


More information about the Digitalmars-d-learn mailing list