callback craziness

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 8 02:13:28 PDT 2016


On 08/08/2016 02:42 AM, Engine Machine wrote:
> So, what about passing in the lambda verses the temp variable?

Nothing. I didn't see a case where it worked one way but not the other. 
If you have code where adding a variable makes things work, please post 
a complete test case.

[...]
> My code is as follows and I cannot get 0 parameters working nor passing
> the function in directly. These were my original questions to begin with
> and haven't been answered. Here is the code I am using.
>
> alias callback(Args...) = @nogc void function(string, int, Args);
> @nogc public void foo(Args...)(callback!Args c, Args args, int x) { }
>
> foo((string s, int i) { }, 1);
>
> does not work
>
> nor does
>
> foo!()((string s, int i) { }, 1);

The second one does work. https://dpaste.dzfl.pl/212b467c62a4

> Ultimately it would also be nice if the template parameters were deduced
> automatically instead of having to specify them. The compiler should be
> able to figure out the types supplied parameters.
>
> e.g.,
>
> foo((string s, int i, T x) { }, someT, 1);
>
> instead of
>
> foo!(T)((string s, int i, T x) { }, someT, 1);
>
> should it not?

I think the `callback` template is the problem here.

Template instantiation is a one way street. You generally can't answer 
the question "With what arguments would template Foo need to be 
instantiated to get result Bar?". For starters, multiple different 
arguments may map to the same result, making the reverse ambiguous.

It works when you skip the `callback` template and put the 
function/delegate type directly into the signature:

----
@nogc public void foo(Args...)(void function(string, int, Args) @nogc c,
     Args args, int x)
{}

void main() @nogc
{
     foo((string s, int i) { }, 1);

     alias T = float;
     T someT;
     foo((string s, int i, T x) { }, someT, 1);
}
----


More information about the Digitalmars-d-learn mailing list