www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - lambda syntax with curly braces

reply "sigod" <sigod.mail gmail.com> writes:
 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 */ }`.
Aug 10 2015
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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); });
Aug 10 2015
parent reply "sigod" <sigod.mail gmail.com> writes:
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.
Aug 10 2015
parent reply "bachmeier" <no spam.com> writes:
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.
Aug 10 2015
parent reply "sigod" <sigod.mail gmail.com> writes:
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.
Aug 10 2015
parent "bachmeier" <no spam.com> writes:
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.
Aug 10 2015