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

template member method #811

Closed
xushiwei opened this issue Sep 19, 2021 · 1 comment
Closed

template member method #811

xushiwei opened this issue Sep 19, 2021 · 1 comment
Labels

Comments

@xushiwei
Copy link
Member

xushiwei commented Sep 19, 2021

In normal way to implement a clone method:

type Sprite struct {
    // ...
}

func (p *Sprite) CloneTo(dest *Sprite) {
   // ...
}

type Foo struct {
    Sprite
    // ...
}

func (p *Foo) Clone() *Foo {
    dest := new(Foo)
    p.Sprite.CloneTo(&dest.Sprite)
    // ...
    return dest
}

Suppose we have template member method:

type Sprite struct {
    // ...
}

// any T who have embedded Sprite member will implement Clone method automatically
func [T Sprite] (p *T) Clone() *T {
    // ...
}
@xushiwei
Copy link
Member Author

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

No branches or pull requests

1 participant