Bug 16672 – Deprecate "block only" delegate syntax

Status
REOPENED
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-11-08T17:06:00Z
Last change time
2024-12-13T18:50:38Z
Assigned to
No Owner
Creator
Steven Schveighoffer
Moved to GitHub: dmd#19205 →

Comments

Comment #0 by schveiguy — 2016-11-08T17:06:00Z
Currently, this code: {int x = 5; return x;} Can either be a block (if written by itself), or a delegate, if used in an expression. e.g.: auto dg = {int x = 5; return x;} This particular ambiguity becomes horrible if combined with the single expression lambda syntax: auto x = (a => {return a;})(1); What this looks like is an immediately called lambda that returns its parameter (a). However, what it *REALLY* returns is a delegate that will return 1 when called. This confusion leads to many problems, as people never want this result. The solution is simple -- deprecate the shortened brace syntax, and require () before the braces. e.g.: auto dg = () {int x = 5; return x; } Without the parens, the above should be an error. The drawback is obviously that one needs to insert parentheses wherever a no-param delegate needs declaring. The cost of doing this, IMO, is low compared to the regular reports we get of the x => {...} syntax "not working".
Comment #1 by nick — 2016-11-10T12:17:58Z
Link to most recent question on the forum: http://forum.dlang.org/post/[email protected] Andrei uncovered this gem: int j; for({j=2; int d = 3; } j+d<7; {j++; d++;}) { } http://forum.dlang.org/thread/[email protected] As he explains, the Initialize part of `for` grammar specially allows BlockStatement, but the Increment part is just an expression. So {j++; d++;} is an expression, *not* a BlockStatement, which parses as a nullary delegate, which is never called! Hence infinite loop. Andrei also mentions an alternative fix: > Another possibility is to disallow an ExpressionStatement that > consists solely of a lambda. There is precedent for that, e.g. > the statement "1 + 1;" is disallowed. -- Andrei
Comment #2 by razvan.nitu1305 — 2022-11-11T12:27:27Z
*** Issue 16632 has been marked as a duplicate of this issue. ***
Comment #3 by schveiguy — 2022-11-11T16:56:11Z
Technically, the single expression lambda syntax problem is fixed, as it's now deprecated.
Comment #4 by nick — 2022-11-13T11:35:42Z
(In reply to Nick Treleaven from comment #1) > int j; > for({j=2; int d = 3; } j+d<7; {j++; d++;}) { This will be caught by issue #23480.
Comment #5 by razvan.nitu1305 — 2022-11-14T11:03:06Z
*** This issue has been marked as a duplicate of issue 23480 ***
Comment #6 by nick — 2022-11-14T13:54:24Z
This issue is not related to the `for` statement. Rather the `for` issue was related to this.
Comment #7 by robert.schadek — 2024-12-13T18:50:38Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19205 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB