How to declare an alias to a function literal type

ParticlePeter via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 12 08:22:48 PST 2016


On Tuesday, 12 January 2016 at 15:57:03 UTC, Marc Schütz wrote:
> 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);
> }

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 ','

Actually, I do use only one param, and not int as well, hence I 
would like the parameter list to be part of the alias.
Your example works though.



More information about the Digitalmars-d-learn mailing list