www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to declare an alias to a function literal type

reply ParticlePeter <ParticlePeter gmx.de> writes:
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. )
Jan 12 2016
next sibling parent reply Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
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); }
Jan 12 2016
parent reply ParticlePeter <ParticlePeter gmx.de> writes:
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.
Jan 12 2016
next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Jan 12 2016
prev sibling parent reply ParticlePeter <ParticlePeter gmx.de> writes:
On Tuesday, 12 January 2016 at 16:22:48 UTC, ParticlePeter wrote:

 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.
This was confusing, lets start fresh: 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 ); Side-note, I use the keyword function to signal that it is a function and not a delegate, thought it is a delegate when not specified. When I pass a parameter to otherFunc I use this syntax for an anonymous function parameter: otherFunc( void function( ref int p1, float p2, ubyte p3 ) { myCode; } ); I would like to alias the function signature above: alias MF = void function( ref int p1, float p2, ubyte p3 ); I can rewrite the definition of otherFunc like this: void otherFunc( MF mf ); But I cannot pass an anonymous function to otherFunc like this: otherFunc( MF { myCode; } ); Thats what I want. Any working example? Ali, I do not pass an existing named function as a pointer. I am also not sure about the lambdas, as I do not return anything, I just want to process data, would that work?
Jan 12 2016
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Jan 12 2016
parent ParticlePeter <ParticlePeter gmx.de> writes:
On Tuesday, 12 January 2016 at 17:03:49 UTC, Ali Çehreli wrote:
 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 }
O.K. so I conclude that writing: void main() { otherFunc(MF { }); } is not possible. At least not with alias, maybe with templates or mixins? In essence something like C #define as in: #define MF function( ref int p1, float p2, ubyte p3 ) Is there some such way?
Jan 12 2016
prev sibling next sibling parent reply Marc =?UTF-8?B?U2Now7x0eg==?= <schuetzm gmx.net> writes:
On Tuesday, 12 January 2016 at 16:55:48 UTC, ParticlePeter wrote:
 I can rewrite the definition of otherFunc like this:
 void otherFunc( MF mf );

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

 Thats what I want. Any working example?
If I understand you correctly (not sure), you would like to write `MF` so that you don't need to specify the parameters in the lambda? That's not possible, because the code inside the lambda needs names for them if it wants to access them, but parameter names are _not_ part of the function type, and therefore the alias doesn't know about them. However, you don't need to specify the full parameter list in the lambda, the names and `ref` are enough: otherFunc( (ref a, ref b, ref c) { /* use a, b, c */ } );
Jan 12 2016
parent ParticlePeter <ParticlePeter gmx.de> writes:
On Tuesday, 12 January 2016 at 17:28:35 UTC, Marc Schütz wrote:
 On Tuesday, 12 January 2016 at 16:55:48 UTC, ParticlePeter 
 wrote:
 [...]
If I understand you correctly (not sure), you would like to write `MF` so that you don't need to specify the parameters in the lambda? That's not possible, because the code inside the lambda needs names for them if it wants to access them, but parameter names are _not_ part of the function type, and therefore the alias doesn't know about them. However, you don't need to specify the full parameter list in the lambda, the names and `ref` are enough: otherFunc( (ref a, ref b, ref c) { /* use a, b, c */ } );
This is already quite useful, thanks.
Jan 12 2016
prev sibling parent anonymous <anonymous example.com> writes:
On 12.01.2016 17:55, ParticlePeter wrote:
 When I pass a parameter to otherFunc I use this syntax for an anonymous
 function parameter:

 otherFunc( void function( ref int p1, float p2, ubyte p3 ) { myCode; } );
You don't. That's not valid code. You can be using this: otherFunc( function void ( ref int p1, float p2, ubyte p3 ) { myCode; } ); Note the different position of the `function` keyword.
Jan 12 2016
prev sibling next sibling parent reply Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
V Tue, 12 Jan 2016 15:41:02 +0000
ParticlePeter via Digitalmars-d-learn
<digitalmars-d-learn puremagic.com> napsáno:

 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. )
alias void MF(int i);
Jan 12 2016
parent ParticlePeter <ParticlePeter gmx.de> writes:
On Tuesday, 12 January 2016 at 16:00:37 UTC, Daniel Kozak wrote:
 V Tue, 12 Jan 2016 15:41:02 +0000
 ParticlePeter via Digitalmars-d-learn
 <digitalmars-d-learn puremagic.com> napsáno:

 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. )
alias void MF(int i);
That does not work: alias void MF(int i); MF mf; // Error: variable mf cannot be declared to be a function
Jan 12 2016
prev sibling next sibling parent anonymous <anonymous example.com> writes:
On 12.01.2016 16:41, ParticlePeter wrote:
 // alias MF = void function( int i );  // not working
 // alias void function( int i ) MF;  // not working
These are both fine. The first one is the preferred style.
 MF myFunc;
 myFunc = MF { myCode };
This line doesn't work. Function literals don't take a type before the curly braces. They have their own syntax. See http://dlang.org/spec/expression.html (search for "Function Literals"). Since most of the stuff in function literals can be inferred from the context, something as simple as this works: myFunc = (i) { myCode };
Jan 12 2016
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 01/12/2016 07:41 AM, ParticlePeter wrote:

 Please, if possible, also show me where I should have found the answer
 (D Reference, Alis book
It is not used with a function literal but searching for 'alias' below yields something close: :) http://ddili.org/ders/d.en/lambda.html <quote> Function pointer syntax is relatively harder to read; it is common to make code more readable by an alias: alias CalculationFunc = int function(char, double); That alias makes the code easier to read: CalculationFunc ptr = &myFunction; As with any type, auto can be used as well: auto ptr = &myFunction; </quote> As others have already said, &myFunction can be replaced with a lambda. Shamefully without compiling: auto ptr = (char, double) => 42; Ali
Jan 12 2016