[Issue 7176] Lambda => syntax for function and methods too
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Dec 30 03:34:47 UTC 2020
https://issues.dlang.org/show_bug.cgi?id=7176
Dlang Bot <dlang-bot at dlang.rocks> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution|--- |FIXED
--- Comment #22 from Dlang Bot <dlang-bot at dlang.rocks> ---
dlang/dmd pull request #11833 "Implement shortened methods with => syntax" was
merged into master:
- e6850f8a241192e24869f9b7ad608b52706e1aa5 by Nicholas Lindsay Wilson:
Fix issue 7176: Implement shortened methods with => syntax
For function literals, D offers a short syntax: `x => y`,
which expands to `function (x) { return y; }`. However, for
normal function definitions, there was no such shortening.
For various applications, there can be more syntax than
meaning, such as property accessors.
This commit changes that by expanding the same `=>` syntax
we already have to work in declarations as well. It expands
to the same thing: `int foo() => x;` is simply
`int foo() { return x; }`; this is just shortened syntax.
Combined with existing rules like auto returns, a property
getter can be be as simple as `@property x() => x_;` or a
range-based pipeline may appear like
`auto common_operation() => this[].sort.uniq;` giving D's
existing functional strengths more syntax sugar.
C# has demonstrated the utility of such shortened methods
since its version 7. However, while C# allows it for
constructors as well, this commit will parse it but fail
in semantic because you cannot return a value from a
constructor. I am not convinced it is worth special-casing
the simple "=> y ALWAYS means "{ return y; }" rule for
this case and thus left it alone.
https://github.com/dlang/dmd/pull/11833
--
More information about the Digitalmars-d-bugs
mailing list