1
2
3
4
5
6
7
8
9
10
11 package main
12
13 import "fmt"
14
15 type E interface{}
16
17 func f[T any](x T) interface{} {
18 var i interface{} = x
19 return i
20 }
21
22 func fs[T any](x T) interface{} {
23 y := []T{x}
24 var i interface{} = y
25 return i
26 }
27
28 func g[T any](x T) E {
29 var i E = x
30 return i
31 }
32
33 type C interface {
34 foo() int
35 }
36
37 type myInt int
38
39 func (x myInt) foo() int {
40 return int(x + 1)
41 }
42
43 func h[T C](x T) interface{ foo() int } {
44 var i interface{ foo() int } = x
45 return i
46 }
47 func i[T C](x T) C {
48 var i C = x
49 return i
50 }
51
52 func j[T C](t T) C {
53 return C(t)
54 }
55
56 func js[T any](x T) interface{} {
57 y := []T{x}
58 return interface{}(y)
59 }
60
61 func main() {
62 if got, want := f[int](7), 7; got != want {
63 panic(fmt.Sprintf("got %d want %d", got, want))
64 }
65 if got, want := fs[int](7), []int{7}; got.([]int)[0] != want[0] {
66 panic(fmt.Sprintf("got %d want %d", got, want))
67 }
68 if got, want := g[int](7), 7; got != want {
69 panic(fmt.Sprintf("got %d want %d", got, want))
70 }
71 if got, want := h[myInt](7).foo(), 8; got != want {
72 panic(fmt.Sprintf("got %d want %d", got, want))
73 }
74 if got, want := i[myInt](7).foo(), 8; got != want {
75 panic(fmt.Sprintf("got %d want %d", got, want))
76 }
77 if got, want := j[myInt](7).foo(), 8; got != want {
78 panic(fmt.Sprintf("got %d want %d", got, want))
79 }
80 if got, want := js[int](7), []int{7}; got.([]int)[0] != want[0] {
81 panic(fmt.Sprintf("got %d want %d", got, want))
82 }
83 }
84
View as plain text