Skip to content

Implement shortened methods with => syntax#11833

Merged
dlang-bot merged 1 commit intodlang:masterfrom
adamdruppe:shortened_methods
Dec 30, 2020
Merged

Implement shortened methods with => syntax#11833
dlang-bot merged 1 commit intodlang:masterfrom
adamdruppe:shortened_methods

Conversation

@adamdruppe
Copy link
Contributor

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.

@dlang-bot
Copy link
Contributor

dlang-bot commented Oct 8, 2020

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 verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

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

Auto-close Bugzilla Severity Description
7176 enhancement Lambda => syntax for function and methods too

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#11833"

@adamdruppe adamdruppe force-pushed the shortened_methods branch 2 times, most recently from bde26e9 to deab5d5 Compare October 8, 2020 19:04
@12345swordy
Copy link
Contributor

You need a preview switch for this.

@thewilsonator thewilsonator added the Review:Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org label Oct 8, 2020
@thewilsonator
Copy link
Contributor

this commit will parse it but fail in semantic because you cannot return a value from a constructor

Is it possible to detect this and give a nice error message?

@adamdruppe
Copy link
Contributor Author

No need; there already is one:

class A { this() => 7; }

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@thewilsonator
Copy link
Contributor

Have you made a forum post about this yet? If so please link.

@UplinkCoder
Copy link
Member

🤷

@Geod24
Copy link
Member

Geod24 commented Oct 9, 2020

Been wanting this a few times, so obviously 👍

Few things:

  • Are there any context where the syntax could be ambiguous ? auto comes to mind (delegate member variable vs function).
  • Need spec PR, obviously;
  • I'd also open a forum thread, as discussed;
  • This probably needs either a -preview or a DIP;
  • Does this work with mixins ?

@UplinkCoder
Copy link
Member

It's a visible language change.
Needs a DIP.

@adamdruppe
Copy link
Contributor Author

adamdruppe commented Oct 9, 2020 via email

@adamdruppe
Copy link
Contributor Author

adamdruppe commented Oct 9, 2020 via email

@adamdruppe
Copy link
Contributor Author

anyone what's up with the azure-pipelines Azure pipelines (Windows_LDC_MinGW win32-ldc) test?

@thewilsonator
Copy link
Contributor

I've seen it on a few other PRs.

@adamdruppe
Copy link
Contributor Author

@UplinkCoder
Copy link
Member

UplinkCoder commented Oct 9, 2020 via email

@rainers
Copy link
Member

rainers commented Oct 9, 2020

Druntime for mingw printf format specifiers are wrong

The respective build is not done with -de so the compiler doesn't error out. unittest.exe is built successfully and is run, but crashes. IIRC DM make misinterprets the exit code and reports not found.

@WalterBright WalterBright added the Review:Needs Changelog A changelog entry needs to be added to /changelog label Oct 10, 2020
@adamdruppe
Copy link
Contributor Author

Old bugzilla link came up on the forum:
https://issues.dlang.org/show_bug.cgi?id=7176

can perhaps use that for the changelog.

@rainers rainers mentioned this pull request Oct 11, 2020
@CyberShadow
Copy link
Member

You need a preview switch for this.

I thought we only need preview switches for breaking changes. Why would strict additions need a compiler switch?

@wilzbach
Copy link
Contributor

You need a preview switch for this.

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.

@12345swordy
Copy link
Contributor

I concur. This needs a preview switch.

@Imperatorn
Copy link
Contributor

Just be sure to add documentation and examples so it's clear to users how to use it. Other than that, 10 x 👍

@andre2007
Copy link

@adamdruppe any chance you can continue with this pr (add the missing parts)? This pr is highly interesting.

@adamdruppe
Copy link
Contributor Author

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.

@PetarKirov
Copy link
Member

PetarKirov commented Dec 20, 2020

Can't the changelog be generated from the commit message? I don't know how these things work.

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.

@Imperatorn
Copy link
Contributor

So, what's the status on this one now? Ready for merge? Anything left to be done?

@12345swordy
Copy link
Contributor

You need see the red tags, as there they are very impotent.

@thewilsonator
Copy link
Contributor

Test 'compilable/previewhelp.d' failed: 
...
diff:
----
   =rvaluerefparam   enable rvalue arguments to ref parameters
   =nosharedaccess   disable access to shared memory objects
   =in               `in` on parameters means `scope const [ref]` and accepts rvalues
+  =shortenedMethods allow use of => for methods and top-level functions in addition to lambas
   =inclusiveincontracts 'in' contracts of overridden methods must be a superset of parent contract
----

@thewilsonator thewilsonator removed the Review:Needs Changelog A changelog entry needs to be added to /changelog label Dec 30, 2020
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.
@adamdruppe
Copy link
Contributor Author

@thewilsonator thanks for the help things like this are never high on my todo list

@dlang-bot dlang-bot merged commit ed994a4 into dlang:master Dec 30, 2020
@Geod24
Copy link
Member

Geod24 commented Dec 30, 2020

🎉

@WalterBright
Copy link
Member

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

@Geod24
Copy link
Member

Geod24 commented Dec 30, 2020

@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.
Regarding the spec PR, I usually ask for it (and I did) but most people tend to forget. At least this one is behind a -preview switch.

@WalterBright
Copy link
Member

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.

@adamdruppe
Copy link
Contributor Author

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

@Imperatorn
Copy link
Contributor

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)

@schveiguy
Copy link
Member

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.

@12345swordy
Copy link
Contributor

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.

@Imperatorn
Copy link
Contributor

Who can update the spec? One week until release 👀

@Geod24
Copy link
Member

Geod24 commented Feb 22, 2021

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.

@Imperatorn
Copy link
Contributor

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

@schveiguy
Copy link
Member

#12241

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merge:auto-merge Review:Needs Spec PR A PR updating the language specification needs to be submitted to dlang.org Severity:Enhancement Severity:New Language Feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.