Skip to content

proposal: Go 2: allow default values in methods #38484

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
furkilic opened this issue Apr 16, 2020 · 4 comments
Closed

proposal: Go 2: allow default values in methods #38484

furkilic opened this issue Apr 16, 2020 · 4 comments
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Milestone

Comments

@furkilic
Copy link

Creating this proposal as I couldn't find any proposal regarding this enhancement.

We often use method overloading in our work :

func (m MyType) something(s string) {
  m.somethingWithI(s, 1)
}
func (m MyType) somethingWithI(s string, i int) {
  m.somethingWithIAndObjAndF(s, i, MyOtherType{}, 0.0)
}
func (m MyType) somethingWithIAndObjAndF(s string, i int, obj MyOtherType, f float64) {
	// s => dynamic
	// i => default 1
	// obj => default MyOtherType{}
	// f => default 0.0
}

// usage
m := MyType{}
m.something("Hello")
m.somethingWithI("Hello", 2)
m.somethingWithIAndObjAndF("Hello", 1, MyOtherType{}, 0.1)

It will be pleasent to have something as such :

func (m MyType) something(s string, i int:= 1, obj MyOtherType := MyOtherType{} , f float64 := 0.0) {
  	// s => dynamic
	// i => default 1
	// obj => default MyOtherType{}
	// f => default 0.0
}

// or make all the paremeters on the right by default once one of them is defaulted
func (m MyType) something(s string, i int:= 1, obj MyOtherType, f float64) {
  	// s => dynamic
	// i => default 1
	// obj => default MyOtherType{}
	// f => default 0.0
}

m:= MyType{}
m.something("Hello")
m.something("Hello", 2)
m.something("Hello", 1, MyOtherType{}, 0.1)
@gopherbot gopherbot added this to the Proposal milestone Apr 16, 2020
@ALTree
Copy link
Member

ALTree commented Apr 16, 2020

Optional arguments were proposed and rejected here #1675.

It appears that the proposal was rejected for being fundamentally at odds with the foundational design goals of Go. I'm not sure much has changed in that regard.

@ghost
Copy link

ghost commented Apr 16, 2020

this is against go's philosophy

@zigo101
Copy link

zigo101 commented Apr 16, 2020

we can use variadic parameters to simulate default argument values, though which is not perfect.

@bradfitz bradfitz added the LanguageChange Suggested changes to the Go language label Apr 16, 2020
@ianlancetaylor ianlancetaylor changed the title proposal: allow default values in methods proposal: Go 2: allow default values in methods Apr 17, 2020
@ianlancetaylor ianlancetaylor added the v2 An incompatible library change label Apr 17, 2020
@ianlancetaylor
Copy link
Contributor

Thanks for the proposal. This does seem to be the same as #1675, which was declined. Closing as a dup.

@golang golang locked and limited conversation to collaborators Apr 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

6 participants