delegates, lambdas and functions pitfall

Lodovico Giaretta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 5 05:32:49 PDT 2016


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.


More information about the Digitalmars-d-learn mailing list