Issue 17951 - Deprecate Shorthand Lambda Followed by Brace
Summary: Deprecate Shorthand Lambda Followed by Brace
Status: RESOLVED DUPLICATE of issue 16001
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-10-29 16:38 UTC by Bolpat
Modified: 2021-07-09 00:37 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Bolpat 2017-10-29 16:38:13 UTC
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 Bolpat 2017-10-29 16:53:55 UTC
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/ecwfiderxbfqzjcyymkg@forum.dlang.org
https://forum.dlang.org/thread/ihsmxiplprxwlqkgwswc@forum.dlang.org
https://forum.dlang.org/thread/qsayoktyffczskrnmgxu@forum.dlang.org
Comment 2 Dennis 2021-07-09 00:37:31 UTC

*** This issue has been marked as a duplicate of issue 16001 ***