Bug 17951 – Deprecate Shorthand Lambda Followed by Brace

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-10-29T16:38:13Z
Last change time
2021-07-09T00:37:31Z
Assigned to
No Owner
Creator
Bolpat

Comments

Comment #0 by qs.il.paperinik — 2017-10-29T16:38:13Z
Disallow braced lambda after =>. Concrete, for the D grammar people, if in a Lambda the arrow is followed by a FunctionLiteral which is just a FunctionLiteralBody, this will be an error. It applies not when the function after => is called, e.g. (..) => { .. }(), because then the "thing" after => is a PostfixExpression (as is any function call), not a FunctionLiteralBody.[1] This would prevent many C# newbie errors. It is almost never correct. The workarounds are simple: 1. Use () before the brace. 2. Use function/delegate before the brace 3. Use braces instead of =>, e.g. (..) { return { .. }; } instead of (..) => { .. } The compiler should warn you that => { .. } introduces two nested lambdas and because probably one actually wants just one, suggests to delete the arrow. In case of desired nested lambdas, the recommended fix is 2. to make it obvious to anyone reading the code. Option 1. is possible but not that desirable. Option 3 should definitely not be recommended by the compiler as it is very lengthy. [1] https://dlang.org/spec/expression.html#FunctionLiteral As this is potentially breaking code, we'd need the full deprecation process. Fortunately, nested lambdas are rarely used, so the amount of broken code is small if any.
Comment #1 by qs.il.paperinik — 2017-10-29T16:53:55Z
The issue is being discussed on the forum regularly by newcomers and even regular D users. I didn't search for them myself, but here are some: https://forum.dlang.org/thread/[email protected] https://forum.dlang.org/thread/[email protected] https://forum.dlang.org/thread/[email protected]
Comment #2 by dkorpel — 2021-07-09T00:37:31Z
*** This issue has been marked as a duplicate of issue 16001 ***