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

Open
JavinYang opened this issue Oct 28, 2019 · 7 comments
Open

proposal: Go 2: Discard methods #35206

JavinYang opened this issue Oct 28, 2019 · 7 comments

Comments

@JavinYang
Copy link

@JavinYang 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
@gopherbot gopherbot added the Proposal label 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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Author

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

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

This comment has been minimized.

Copy link

@go101 go101 commented Oct 28, 2019

type discard struct {
    fly int
}

type ostrich struct {
    bird
    discard
}
@JavinYang

This comment has been minimized.

Copy link
Author

@JavinYang JavinYang commented Oct 28, 2019

type discard interface {
    fly()
}

type ostrich struct {
    bird
    discard
}

It seems that interface is a better choice.

@go101

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

@urandom urandom commented Oct 29, 2019

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

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 19, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.