Thread overview |
---|
August 10, 2015 lambda syntax with curly braces | ||||
---|---|---|---|---|
| ||||
From docs:
> The following part => AssignExpression is rewritten to FunctionLiteralBody:
> { return AssignExpression ; }
So, I wonder what happens when curly braces already in place?
Consider this example:
```
import std.algorithm;
import std.stdio;
void main() {
[1,2,3,4,5]
.each!(a => { // remove `=>` and you'll get output
writeln(a);
});
}
```
This code compiles and doesn't output anything. Which is very counterintuitive for me, because my main experience with lambdas was in C#. Where it's perfectly fine to write `identifiers => { /* some code */ }`.
|
August 10, 2015 Re: lambda syntax with curly braces | ||||
---|---|---|---|---|
| ||||
Posted in reply to sigod | On Monday, 10 August 2015 at 13:57:50 UTC, sigod wrote: > From docs: >> The following part => AssignExpression is rewritten to FunctionLiteralBody: >> { return AssignExpression ; } > > So, I wonder what happens when curly braces already in place? It does exactly what that says: rewrites it to (a) { return { writeln(a); }; } which is returning a delegate. > This code compiles and doesn't output anything. So your code passed a delegate that returned a delegate to each. Since the one returned wasn't called, the writeln never happened. If you call it like so: [1,2,3,4,5] .each!(a => { writeln(a); }()); // added parens call the returned delegate then you see it. The => thing in D is meant only for trivial, single line things. If you want multiple lines, that's where the {} syntax comes in with no need for the =>. .each!( (a) { writeln(a); }); |
August 10, 2015 Re: lambda syntax with curly braces | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Monday, 10 August 2015 at 14:05:30 UTC, Adam D. Ruppe wrote:
> On Monday, 10 August 2015 at 13:57:50 UTC, sigod wrote:
>> [...]
>
> It does exactly what that says: rewrites it to
>
> (a) {
> return {
> writeln(a);
> };
> }
>
>
> which is returning a delegate.
>
>> [...]
>
> So your code passed a delegate that returned a delegate to each. Since the one returned wasn't called, the writeln never happened.
>
> If you call it like so:
>
> [1,2,3,4,5]
> .each!(a => {
> writeln(a);
> }()); // added parens call the returned delegate
>
> then you see it.
>
>
>
>
> The => thing in D is meant only for trivial, single line things. If you want multiple lines, that's where the {} syntax comes in with no need for the =>.
>
> .each!( (a) {
> writeln(a);
> });
I see. But it's really counter intuitive after working with C#. Probably documentation should stress out the difference.
Thanks, Adam.
|
August 10, 2015 Re: lambda syntax with curly braces | ||||
---|---|---|---|---|
| ||||
Posted in reply to sigod | On Monday, 10 August 2015 at 15:05:55 UTC, sigod wrote: > I see. But it's really counter intuitive after working with C#. Probably documentation should stress out the difference. > > Thanks, Adam. I assume you mean this page: http://dlang.org/expression.html There's an "Improve this page" button in the upper right corner. It's very easy to recommend a change. |
August 10, 2015 Re: lambda syntax with curly braces | ||||
---|---|---|---|---|
| ||||
Posted in reply to bachmeier | On Monday, 10 August 2015 at 16:02:31 UTC, bachmeier wrote:
> On Monday, 10 August 2015 at 15:05:55 UTC, sigod wrote:
>> I see. But it's really counter intuitive after working with C#. Probably documentation should stress out the difference.
>>
>> Thanks, Adam.
>
> I assume you mean this page:
>
> http://dlang.org/expression.html
>
> There's an "Improve this page" button in the upper right corner. It's very easy to recommend a change.
Good point.
But I seldom do this because English isn't my native language.
|
August 10, 2015 Re: lambda syntax with curly braces | ||||
---|---|---|---|---|
| ||||
Posted in reply to sigod | On Monday, 10 August 2015 at 16:15:40 UTC, sigod wrote:
> Good point.
>
> But I seldom do this because English isn't my native language.
Your English looks fine to me. Close enough to native that I can't tell the difference.
|
Copyright © 1999-2021 by the D Language Foundation