How to declare an alias to a function literal type

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 12 08:30:10 PST 2016


On 01/12/2016 08:22 AM, ParticlePeter wrote:
 > On Tuesday, 12 January 2016 at 15:57:03 UTC, Marc Schütz wrote:

 > Not what I wanted, I wanted the parameter to be part of the alias:
 > myFunc = MF { ... }
 >
 > I want to pass such a function to another function:
 >
 > alias MF = void function(int i);
 > void otherFunc( void function( int ) mf );
 > otherFunc( MF { ... } );      // Getting Error: found '{' when expecting

I've added otherFunc(MF) to Marc Schütz's program:

alias MF = void function(int i);

void otherFunc(MF func) {
     func(42);
}

void main() {
     import std.stdio;
     MF myFunc;
     // you can also use the full `function(int i) { ... }` in the next line
     myFunc = (i) { writeln("i = ", i); };
     myFunc(42);

     otherFunc((i) { writefln("otherFunc called me with %s", i); });
}

Ali



More information about the Digitalmars-d-learn mailing list