digitalmars.D.learn - lambda syntax with curly braces
- sigod (17/20) Aug 10 2015 So, I wonder what happens when curly braces already in place?
- Adam D. Ruppe (22/28) Aug 10 2015 It does exactly what that says: rewrites it to
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 /* some code */ }`.
Aug 10 2015
On Monday, 10 August 2015 at 13:57:50 UTC, sigod wrote:From docs:It does exactly what that says: rewrites it to (a) { return { writeln(a); }; } which is returning a delegate.The following part => AssignExpression is rewritten to FunctionLiteralBody: { return AssignExpression ; }So, I wonder what happens when curly braces already in place?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
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:Probably documentation should stress out the difference. Thanks, Adam.[...]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); });
Aug 10 2015
On Monday, 10 August 2015 at 15:05:55 UTC, sigod wrote: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
On Monday, 10 August 2015 at 16:02:31 UTC, bachmeier wrote:On Monday, 10 August 2015 at 15:05:55 UTC, sigod wrote:Good point. But I seldom do this because English isn't my native language.I see. But it's really counter intuitive after working with 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
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