Skip to content

Add explicit return type to some methods #4937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

kmizu
Copy link

@kmizu kmizu commented Aug 13, 2018

I think the code is more readable if a return type of a method is not omitted generally

Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

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

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Have an awesome day! ☀️

@Blaisorblade
Copy link
Contributor

Hi! Thanks for your PR, but how have you picked which methods to add a return type for? That's really not obvious to me, nor it is whether these methods need a return type.

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

I don't have a strong opinion but would argue that adding : Boolean to an is.. method is redundant. Similarly for : Unit. For the others, the change looks like an improvement in legibility.

@kmizu
Copy link
Author

kmizu commented Aug 17, 2018

@Blaisorblade Hi. I think all public methods (private in some cases) should have explicit return type. in general. The reasons are below:

  1. Return type inference may change method signature depending on implementation details. If these methods are called from different compilation unit, such accidental changes break binary compatibiliy and generate unneeded recompilation.
  2. Depending on implementation details, very complex return type maybe inferred. Such a method is difficult to use.
  3. If a return type is not inferred and written explicitly, the method user can call it without seeing its implementation.
  4. adding explicit return type is low cost and have many merits.

In this PR, I picked several methods that the return types are inferred experimentally. If it's permitted, I'll try to add explicit return type to all public methods.

@odersky I think : Boolean to an is ... is not redundant. the consistency between the method implementation and the return type is guaranteed by : Boolean. Similarly for : Unit (in some cases, Anys were inferred accidentally).

@Blaisorblade
Copy link
Contributor

Agree with most of those arguments, except that there's no binary compatibility to speak of (tho unneeded recompilation stands). Let's see what the others say — e.g. @allanrenucci, @smarter, what do you think?

@allanrenucci
Copy link
Contributor

I usually only add explicit return type to public members. I can see value in adding explicit return type to private/local side effect-full methods that return Unit.

However in general, I would not add explicit return type to private/local methods (and local variables). These methods are usually fairly small and it makes the code more verbose and sometime less readable.

But that's my opinion 😄

@kmizu
Copy link
Author

kmizu commented Aug 21, 2018

@allanrenucci I agree with you opinion (should not add explict return type to private/local methods).

Copy link
Contributor

@allanrenucci allanrenucci left a comment

Choose a reason for hiding this comment

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

Hi @kmizu! We decided that we would accept a PR that adds explicit return type to all public methods. Note that you should be able to use scalafix to automate this task.

This PR adds explicit return type to local and private members which is not desired.

@OlivierBlanvillain
Copy link
Contributor

It's been a month... @kmizu If you plan to continue working on this PR please reopen (or make a new one), I will close it for now.

@kmizu
Copy link
Author

kmizu commented Dec 31, 2018

@OlivierBlanvillain I'm very sorry to forget it. Later, I'll continue working on this PR and reopen this PR (or make a new one).

@smarter
Copy link
Member

smarter commented Dec 31, 2018

Hi @kmizu, thanks for your work but note that meanwhile we've already added explicit result types everywhere in #5203 so this particular PR shouldn't be needed anymore.

@kmizu
Copy link
Author

kmizu commented Dec 31, 2018

Hi, @smarter. I understand that this PR shouldn't be needed anymore. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants