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 09:03:49 PST 2016


On 01/12/2016 08:55 AM, ParticlePeter wrote:

 > I have a function "otherFunc" which takes a function with lots of
 > parameters as argument:
 >
 > void otherFunc( void function( ref int p1, float p2, ubyte p3, ... ) 
mf );

Ok.

 > otherFunc( void function( ref int p1, float p2, ubyte p3 ) { myCode; } );

Ok.

 > alias MF = void function( ref int p1, float p2, ubyte p3 );

Ok.

 > I can rewrite the definition of otherFunc like this:
 > void otherFunc( MF mf );

That has the same problem of trying to do this for int:

void foo(int i) {
}

void main() {
     foo(int 42);     // <-- ERROR
}

But you can do this:

     foo(int(42));    // (Relatively new syntax in D.)

 > But I cannot pass an anonymous function to otherFunc like this:
 > otherFunc( MF { myCode; } );

It works with the parentheses as it does for int:

alias MF = void function( ref int p1, float p2, ubyte p3 );

void otherFunc( MF mf ) {
}

void main() {
     otherFunc(MF((ref int, float, ubyte){ }));    // <-- Parens
}

 > not sure about the lambdas, as I do not return anything, I just want to
 > process data, would that work?

Yes, some lambdas do not return anything.

Ali



More information about the Digitalmars-d-learn mailing list