www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17951] New: Deprecate Shorthand Lambda Followed by Brace


          Issue ID: 17951
           Summary: Deprecate Shorthand Lambda Followed by Brace
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: qs.il.paperinik gmail.com

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

[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.

Oct 29