Skip to content

Instantly share code, notes, and snippets.

@cep21
Last active March 20, 2020 16:06
Show Gist options
  • Save cep21/ae839dd6a0499cdaf0eb68b06d72fcff to your computer and use it in GitHub Desktop.
Save cep21/ae839dd6a0499cdaf0eb68b06d72fcff to your computer and use it in GitHub Desktop.
Example of interface wrapping erasure
package main
// Given an interface
type I interface {
Func()
}
// And another interface
type I2 interface {
Another()
}
// And a struct that implements both
type S struct {
}
var _ I = &S{}
var _ I2 = &S{}
func (s *S) Func() {}
func (s *S) Another() {}
// If you want to augment an interface (but didn't know about I2)
type IChain struct {
I
}
func Augment(i I) I {
return &IChain{i}
}
func main() {
var s I
s = &S{}
_ = s.(I2)
s = Augment(s)
// You lose your implementation of the base interface
_ = s.(I2)
}
@colm-anseo
Copy link

*lose

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment