delegates, lambdas and functions pitfall

dom via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 5 09:11:23 PDT 2016


On Monday, 5 September 2016 at 12:32:49 UTC, Lodovico Giaretta 
wrote:
> On Monday, 5 September 2016 at 12:15:35 UTC, dom wrote:
>> [...]
>
> You misunderstood the error message and the lambda syntax (it 
> also happened to me the first time).
>
> The grammar says that you can use one of these syntaxes:
>
> 1) `(arguments) {block of code}`
>
> 2) `(arguments) => expression`, which is equivalent to 
> `(arguments) {return expression;}`
>
> 3) `{block of code}`, which is equivalent to `(){block of 
> code}`.
>
> So if you write `(arguments) => {block of code}`, this is 
> equivalent to (see rule 2) `(arguments) { return {block of 
> code}; }`.
>
> And because of rule 3, it becomes `(arguments) { return 
> (){block of code} }`. So what you have is a function that 
> returns a function. That's why it does not match your signature.
>
> The fact that the compiler also adds nothrow, @nogc, @safe is 
> not important in this case, as those attributes can be 
> implicitly casted away.

thank you for that detailed answer.




More information about the Digitalmars-d-learn mailing list