Implement shortened methods with => syntax#11833
Conversation
|
Thanks for your pull request and interest in making D better, @adamdruppe! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + dmd#11833" |
bde26e9 to
deab5d5
Compare
|
You need a preview switch for this. |
Is it possible to detect this and give a nice error message? |
|
No need; there already is one:
cool.d(2): Error: cannot return expression from constructor |
src/dmd/parse.d
Outdated
| nextToken(); | ||
| f.fbody = new AST.ReturnStatement(returnloc, parseExpression()); | ||
| check(TOK.semicolon); | ||
| f.endloc = endloc; |
There was a problem hiding this comment.
I think I did this wrong. I just copied from below but p sure this is specifically for }.... should I instead set it to token.loc for the semicolon?
There was a problem hiding this comment.
Seems reasonable to me, and I can't think of any more appropriate token to point it at. It seems to be used only for line mappings for debug info (and I supposed DMD as a Library for whatever).
You should probably update this comment too.
There was a problem hiding this comment.
That comment is the one on the parser class, this is on the f object which is the FuncDeclaration class. (It was that comment that tipped me I was doing the wrong thing though.)
|
Have you made a forum post about this yet? If so please link. |
|
🤷 |
|
Been wanting this a few times, so obviously 👍 Few things:
|
|
It's a visible language change. |
|
On Thu, Oct 08, 2020 at 09:14:47PM -0700, Nicholas Wilson wrote:
Have you made a forum post about this yet? If so please link.
No, this was just from a short chat on the discord. People were talking
about the idea, I said that should be easy to implement, they said "lol
prove it" and i was like "lol k".
We can make one in a bit though.
|
|
On Fri, Oct 09, 2020 at 04:34:15AM -0700, Mathias LANG wrote:
- Are there any context where the syntax could be ambiguous ? `auto` comes to mind (delegate member variable vs function).
I very much doubt it; it should have failed the tests if it was.
The key difference is the parens.
auto a = () => 4; // parens on rhs of =, a is a variable assigned a
delegate
auto c() => 5; // parens on lhs of =, c is a function
The parens after is what the parser looks for to determine it is a
declaration which is then on a different branch than expression parsing.
Since the latter is always illegal in today's D, it is a purely additive
change and should break nothing. The auto tester seems to agree, aside
from what appears to be spurious nonsense in the (Windows_LDC_Debug
x64-debug-ldc) thing...
- Does this work with mixins ?
Can't imagine why it wouldn't. A quick test of
mixin("auto c() => 5;");
seems to agree.
|
deab5d5 to
6b15744
Compare
|
anyone what's up with the azure-pipelines Azure pipelines (Windows_LDC_MinGW win32-ldc) test? |
|
I've seen it on a few other PRs. |
|
Druntime for mingw printf format specifiers are wrong
…On Fri, Oct 9, 2020, 4:37 PM Nicholas Wilson ***@***.***> wrote:
I've seen it on a few other PRs.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#11833 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAVWSCFDNRSCA562CKSPCLTSJ4N2VANCNFSM4SJEDSAA>
.
|
The respective build is not done with |
|
Old bugzilla link came up on the forum: can perhaps use that for the changelog. |
acd15ce to
6b15744
Compare
I thought we only need preview switches for breaking changes. Why would strict additions need a compiler switch? |
With a -preview switch it can be merged right now and formally approved via a DIP later. |
|
I concur. This needs a preview switch. |
|
Just be sure to add documentation and examples so it's clear to users how to use it. Other than that, 10 x 👍 |
|
@adamdruppe any chance you can continue with this pr (add the missing parts)? This pr is highly interesting. |
|
ummm i guess i can look at it later, maybe over the weekend. I don't see any real value in the preview switch but it is probably simple enough to do. |
The changelog for each project is generally generated based on two sources:
That said, even if the changelog could be fully auto-generated, based on Bugzilla, we always use manually written changelog entries for more notable changes, such as this one. |
|
So, what's the status on this one now? Ready for merge? Anything left to be done? |
|
You need see the red tags, as there they are very impotent. |
|
52fa7a8 to
6817c29
Compare
6817c29 to
2e8a671
Compare
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.
2e8a671 to
e6850f8
Compare
|
@thewilsonator thanks for the help things like this are never high on my todo list |
|
🎉 |
|
@thewilsonator @Geod24 This is a language change. Why is there no changelog and no spec pull? Both were called out with labels. There's no indication of what the new BNF grammar is. |
|
@WalterBright : I didn't pull, just got the notification, but those are good point. Even though there's an issue for it (so it appears in the changelog), this definitely warrants its oown changelog entry. |
|
We have standards for how a PR should be done. I certainly do not advocate blindly follow those standards, but for deviations there ought to be a decent reason. I expect all those with Pull privileges to help ensure this. If a PR doesn't meet those standards, an offer to help the submitter would be helpful. I'm especially concerned about this because of the many legitimate complaints that the spec is out of date. This problem should be getting better over time, not worse. I added the "Need Changelog" label which was removed without explanation. I know you requested the Spec change, but that was ignored. @12345swordy wrote as well "You need see the red tags, as there they are very impotent." The typo is appropriate. |
|
I don't speak spec and my understanding* is the rebase means the changelog picked it up. But maybe I'll get around to figuring them out but it will be several months, if ever. So if you want those kind of things done, it is far, far, far more efficient for someone who already knows it to do that part. Specialization of labor isn't just for building horseless carriages. I'm not terribly invested in this PR. I just wrote a patch to prove to someone in chat it wasn't hard to implement. It isn't something I'll likely use myself since I avoid new features anyway (my libs promise compatibility with 3+ year old compilers...)
should be linked from this page https://github.com/dlang/dmd/blob/master/CONTRIBUTING.md |
|
While I'd like this to be merged, which it seems it did, we also have to follow some standards. Sometimes there are multiple forces pulling in different directions. At those times, I actually think Walter should have the final word (if it's not completely insane). Let's respect our founder(s) |
|
Just seeing that this was merged. For 2.096.0, this will be in the compiler, it needs a spec udpate and a better changelog entry. |
|
This shouldn't be merge without a spec PR have been made and merged. If the next compiler release contain this feature without a spec update, it will create unnecessarily confusion for new users. |
|
Who can update the spec? One week until release 👀 |
|
Someone needs to make a PR. And this is why we ask for spec PRs before. Once the feature is merged, there's less incentive to do so. |
|
@adamdruppe I think this might fall on you to do. If you cannot do it yourself, provide an alternative solution to how it could/should be done and what people should/have to be involved. |
For function literals, D offers a short syntax:
x => y,which expands to
function (x) { return y; }. However, fornormal 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
=>syntaxwe already have to work in declarations as well. It expands
to the same thing:
int foo() => x;is simplyint 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 arange-based pipeline may appear like
auto common_operation() => this[].sort.uniq;giving D'sexisting 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.