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: Discard methods #35206

Closed
JavinYang opened this issue Oct 28, 2019 · 8 comments
Closed

proposal: Go 2: Discard methods #35206

JavinYang opened this issue Oct 28, 2019 · 8 comments
Labels
Milestone

Comments

@JavinYang
Copy link

JavinYang commented Oct 28, 2019

type bird struct {
}

func (this *bird) eat() {
}

func (this *bird) drink() {
}

func (this *bird) sleep() {
}

func (this *bird) fly() {
}

type ostrich struct {
    bird
}

func (this *ostrich) runAboutWildly() {
}

// Discard fly
func (this *ostrich) ~ fly()
bigbird := ostrich{}
bigbird.sleep()
bigbird.fly() // not found fly

Provides a way to disable method。
Sometime I will inherit a large number of method when I combine a structure, however I want to abandon some of those methods

@gopherbot gopherbot added this to the Proposal milestone Oct 28, 2019
@JavinYang JavinYang changed the title Proposal: Go2: Discard methods proposal: Go 2: Discard methods Oct 28, 2019
@JavinYang JavinYang changed the title proposal: Go 2: Discard methods proposal: Go 2: Discard methods Oct 28, 2019
@hsson
Copy link

hsson commented Oct 28, 2019

IMO a better solution would be to use the delegation pattern:

type ostrich struct {
    b bird
}

func (this *ostrich) eat() {
    this.b.eat()
}

func (this *ostrich) drink() {
    this.b.drink()
}

func (this *ostrich) sleep() {
    this.b.sleep()
}

It's not worth it to convolute the language just to save a few lines of code.

@JavinYang
Copy link
Author

IMO a better solution would be to use the delegation pattern:

type ostrich struct {
    b bird
}

func (this *ostrich) eat() {
    this.b.eat()
}

func (this *ostrich) drink() {
    this.b.drink()
}

func (this *ostrich) sleep() {
    this.b.sleep()
}

It's not worth it to convolute the language just to save a few lines of code.

it is feasible if the number of methods is small, but when the number of methods is large, it will cause lots of work. I will encounter this situation in my actually development, which resulting a large number of duplicate code. Of course, you can design a new parent structure, but it's still a good amount of work.Because the project already largely depend on the parent structure.
Not all structures can be designed perfectly from the very beginning.

@go101
Copy link

go101 commented Oct 28, 2019

type discard struct {
    fly int
}

type ostrich struct {
    bird
    discard
}

@JavinYang
Copy link
Author

type discard interface {
    fly()
}

type ostrich struct {
    bird
    discard
}

It seems that interface is a better choice.

@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange labels Oct 28, 2019
@go101
Copy link

go101 commented Oct 28, 2019

Better from the view of aesthetics.

In fact, the following one is more memory saving:

type discard struct {
    fly struct{}
}

@urandom
Copy link

urandom commented Oct 29, 2019

What is the problem of having more methods than you are using?

@ianlancetaylor
Copy link
Contributor

#35206 (comment) shows a different way to implement this without changing the language. This proposal as is does not have strong support. For these reasons, this is a likely decline. Leaving open for four weeks for final comments.

@griesemer
Copy link
Contributor

No final comments. Closing.

@golang golang locked and limited conversation to collaborators Dec 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants