Better lambdas!!!!!!!!!!

Idan Arye via Digitalmars-d digitalmars-d at puremagic.com
Sat Sep 12 06:33:17 PDT 2015


On Saturday, 12 September 2015 at 10:44:05 UTC, Pierre Krafft 
wrote:
>  myfunc({return "x:"~x~"y:"-y;});
> is infered to mean myfunc((x,y){return "x:"~x~"y:"-y;});
> while
>  myfunc({return "y:"~y~"x:"~x;});
> is infered to mean myfunc((y,x){return "y:"~y~"x:"~x;});
> which is not what I expect since the lambda I want is 
> myfunc((x,y){return "y:"~y~"x:"~x;});
> This can lead to subtle bugs which are very hard to see.

I don't think this is what the OP was suggesting. As far as I 
understand, the suggestion was that the lambda's arguments would 
be inferred from the function argument in higher order's function 
signature - not from the ones used in the lambda's body as you 
suggest.

So, `myfunc` will be declared as:

     void myfunc(string delegate(string x, string y)) { // ...

And when the compiler see `myfunc({return "x:"~x~"y:"-y;});`, 
it'll see that `x` and `y` appear in the function argument's 
definition and match them accordingly. This means that 
`myfunc({/*...*/})` will be inferred to `myfunc((x,y) {/*...*/})` 
no matter what the order of argument usage in the lambda's body 
is - because in the function's signature the arguments are `x` 
and `y` in that order.


More information about the Digitalmars-d mailing list