Hi,
I created a test application (test.d) to learn delegates.

import core.stdc.stdio;

void Multi (int delegate()[] args ...)
{
    foreach (exp; args)
        printf ("%d, ", exp() );
    printf ("\n");
}

void Single (int delegate() exp)
{
    printf ("%d\n", exp());
}

void main()
{
        int x = 5;
        Multi( {return 1;}, {return 2+x;} ); // outputs 1, 7 as expected.
        Multi( 1, 2+x); // same as above.  produces same output.
        Multi( 3+x ); //produces output 8.
        Single( { return 3+x; } ); //produces output 8
        Single( 3+x ); // COMPILATION ERROR pasted towards bottom.
        // Good. But why does Multi(2+x) not have this issue?
        
        int delegate() exp = { return 4+x; };
        printf("%d\n", exp() );
}
        
test.d(22): Error: function test.Single(int delegate() exp) is not callable using argument types (int) test.d(22): cannot pass argument 3 + x of type int to parameter int delegate() exp
make: *** [test.obj] Error 1

I am happy with the statement Single(3+x) producing this compilation error.

But why Multi(3+x) doesn't have this issue? For that, it is allowed as per 'Section 19.16.3.4 Lazy Variadic Functions' of the article https://dlang.org/spec/function.html#closures.

What is the rationale behind not allowing it for the non-array signature?

Regards,
Gopan
  • converting expression to dele... Gopan via Digitalmars-d-learn

Reply via email to