Last active
March 20, 2020 16:06
-
-
Save cep21/ae839dd6a0499cdaf0eb68b06d72fcff to your computer and use it in GitHub Desktop.
Example of interface wrapping erasure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
*lose