Jump to page: 1 2
Thread overview
How to declare an alias to a function literal type
Jan 12, 2016
ParticlePeter
Jan 12, 2016
Marc Schütz
Jan 12, 2016
ParticlePeter
Jan 12, 2016
Ali Çehreli
Jan 12, 2016
ParticlePeter
Jan 12, 2016
Ali Çehreli
Jan 12, 2016
ParticlePeter
Jan 12, 2016
Marc Schütz
Jan 12, 2016
ParticlePeter
Jan 12, 2016
anonymous
Jan 12, 2016
Daniel Kozak
Jan 12, 2016
ParticlePeter
Jan 12, 2016
anonymous
Jan 12, 2016
Ali Çehreli
January 12, 2016
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. )
January 12, 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);
}

January 12, 2016
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);

January 12, 2016
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 };
January 12, 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.

January 12, 2016
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

January 12, 2016
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


January 12, 2016
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

January 12, 2016
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?


January 12, 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

« First   ‹ Prev
1 2