How to declare an alias to a function literal type

Marc Schütz via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 12 07:57:03 PST 2016


On Tuesday, 12 January 2016 at 15:41:02 UTC, ParticlePeter wrote:
> I have a function type and variable and assign a function to it:
>
> void function( int i ) myFunc;
> myFunc = void function( int i ) { myCode; }
>
> How would I declare an alias for void function( int i ) such 
> that the case above would work like this:
>
> // alias MF = void function( int i );  // not working
> // alias void function( int i ) MF;  // not working
>
> MF myFunc;
> myFunc = MF { myCode };
>
> Please, if possible, also show me where I should have found the 
> answer (D Reference, Alis book, etc. )

This works for me:

alias MF = void function(int i);  // works fine - what was your 
error?

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);
}



More information about the Digitalmars-d-learn mailing list