[Issue 16672] New: Deprecate "block only" delegate syntax
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Tue Nov 8 09:06:00 PST 2016
https://issues.dlang.org/show_bug.cgi?id=16672
Issue ID: 16672
Summary: Deprecate "block only" delegate syntax
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: schveiguy at yahoo.com
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".
--
More information about the Digitalmars-d-bugs
mailing list