Skip to content
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

proposal: Go 2: named parameter in function invocation #29673

Closed
viper10652 opened this issue Jan 11, 2019 · 10 comments
Closed

proposal: Go 2: named parameter in function invocation #29673

viper10652 opened this issue Jan 11, 2019 · 10 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@viper10652
Copy link

Context:
Go does not allow specifying the name of function parameters in function invocations like in other languages. I don't understand the reasoning behind this.

Proposal:
allow the function parameter name to be specified explicitly in the function invocation.

func Foo (p1 int, p2 int, p3 int) {}
Foo(p1=1, p2=2, p3=3)

The reason for this is not necessarily because I would like to list them in another order in the invocation.
I don't even mid if the compiler forces the parameters to be specified in the same order as the declaration.

The reason is to avoid errors by making it clear what the intended use for the parameter is.

Remember, the function declaration and function invocation are in different places. They are often written by different people in a team or in different teams.
So when I read the invocation Foo(1,2,3), then I need to go look up the function declaration either in the code or in the documentation to understand what these parameters do. When I read Foo(p1=1, p2=2, p3=3), i don't need to look that up, it gives me all I need.

Also, code evolves. When the definition of function changes, and the function parameters are not named in the function invocation, then there is a potential for unintended bugs. You cannot expect the programmer who needs to modify the function declaration to go fix every invocation in every project (some of which he might not even have privilege to). By changing the name of a parameter, the function programmer can indicate a change in the intended use of the parameter.

When everyone consistently names their parameters during invocations, then a change in the function declaration is immediately caught by the compiler and the API users can fix their code accordingly.

I have seen comments on similar proposals like "I don't see a need for this" do not seem to have much experience with the power of this suggested feature.
IN MY OPINION (and I understand that does not necessarily have to be other people's opinion): it makes code readable, unambiguous and prevents unintended errors. A lot of programmers coming from other programming languages probably agree with me. If you don't see a need for it, then don't use it but at least allow people who do feel a need for it to use it. It's the lack of features like this that makes the transition from Python to Go less palatable for me.

@gopherbot gopherbot added this to the Proposal milestone Jan 11, 2019
@go101
Copy link

go101 commented Jan 11, 2019

dup of #12296?

@ianlancetaylor
Copy link
Contributor

I expect that it's a dup but since that issue is largely based on a Google doc that has been deleted, we can leave this issue open for a bit.

@viper10652 Please consider, for example, @griesemer 's comments in #12296 (comment) .

@ianlancetaylor ianlancetaylor added LanguageChange v2 A language change or incompatible library change labels Jan 11, 2019
@ianlancetaylor
Copy link
Contributor

@viper10652 When presenting a proposal for a language change, it's not enough to explain that the change is useful. Every change proposal is useful; nobody proposes useless ideas. You need to also consider the cost of the change. All changes have a benefit and a cost, and we need to make sure that the benefit outweighs the cost.

Consider also the decision criteria laid out in https://blog.golang.org/go2-here-we-come .

Thanks.

@ianlancetaylor
Copy link
Contributor

Sorry, just one more thing.

By changing the name of a parameter, the function programmer can indicate a change in the intended use of the parameter.

This is an interesting example, because changing the name of a parameter will potentially break every other package that calls that function. It's clear that code that follows anything like the Go 1 compatibility guarantee will not be able to ever change a parameter name. And the theory of the modules approach that is under development is that people will increment the major version each time they make a breaking change. So the suggestion that a programmer can indicate a change in a function by changing the name of a parameter implies that it is likely that people will make breaking changes to their code. But in general we do not want to encourage that. And of course people can already make a breaking change by renaming the function. So this doesn't seem like a strong argument in favor of this proposal.

@griesemer
Copy link
Contributor

griesemer commented Jan 11, 2019

This is essentially a dup of #12296. I'll leave the closing of this issue to the Go 2 proposal reviewers.

@viper10652 Specifically read #12296 (comment) which outlines some of the problems with named function arguments. I don't see them addressed here or even mentioned.

@Davidc2525

This comment has been minimized.

@bcmills

This comment has been minimized.

@ianlancetaylor
Copy link
Contributor

Closing as dup of #12296.

@EnriqueL8
Copy link

Hi, is there an update on this? Have the problems addressed in #12296 been discussed any further? I think this would be an awesome feature!

@ianlancetaylor
Copy link
Contributor

Sorry, these proposals have been declined.

@golang golang locked and limited conversation to collaborators Dec 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

8 participants