package main
-import "reflect"
+import "fmt"
func f[T any](i interface{}) {
switch x := i.(type) {
case T:
- println("T", x)
+ fmt.Println("T", x)
case int:
- println("int", x)
+ fmt.Println("int", x)
case int32, int16:
- println("int32/int16", reflect.ValueOf(x).Int())
+ fmt.Println("int32/int16", x)
case struct{ a, b T }:
- println("struct{T,T}", x.a, x.b)
+ fmt.Println("struct{T,T}", x.a, x.b)
default:
- println("other", reflect.ValueOf(x).Int())
+ fmt.Println("other", x)
}
}
func main() {
f[float64](int8(9))
f[int32](int32(7))
f[int](int32(7))
+ f[any](int(10))
+ f[interface{ M() }](int(11))
}
package main
type I interface{ foo() int }
+type J interface {
+ I
+ bar()
+}
type myint int
type myint32 int32
func (x myint32) foo() int { return int(x) }
+func (x myint32) bar() {}
func f[T I](i I) {
switch x := i.(type) {
f[myint32](myint32(8))
f[myint32](myfloat(7))
f[myint](myint32(9))
+ f[I](myint(10))
+ f[J](myint(11))
+ f[J](myint32(12))
}
package main
type I interface{ foo() int }
+type J interface {
+ I
+ bar()
+}
type myint int
type myint32 int32
func (x myint32) foo() int { return int(x) }
+func (x myint32) bar() {}
func f[T I](i I) {
switch x := i.(type) {
f[myint32](myint32(9))
f[myint](myint32(10))
f[myint](myfloat(42))
+ f[I](myint(10))
+ f[J](myint(11))
+ f[J](myint32(12))
}