January 12, 2016
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 */ } );
January 12, 2016
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?



January 12, 2016
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.
January 12, 2016
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.
1 2
Next ›   Last »