1
2
3
4
5
6
7 package p
8
9
10
11 func Reverse[T any](list []T) {
12 i := 0
13 j := len(list)-1
14 for i < j {
15 list[i], list[j] = list[j], list[i]
16 i++
17 j--
18 }
19 }
20
21 func _() {
22
23 Reverse[int](nil)
24 Reverse[string]([]string{"foo", "bar"})
25 Reverse[struct{x, y int}]([]struct{x, y int}{{1, 2}, {2, 3}, {3, 4}})
26
27
28
29 Reverse([]string{"foo", "bar"})
30 Reverse([]struct{x, y int}{{1, 2}, {2, 3}, {3, 4}})
31
32
33
34
35
36
37 Reverse([]int(nil))
38 }
39
40
41
42 func new[T any]() *T {
43 var x T
44 return &x
45 }
46
47
48
49
50
51
52 var _ = new[int]()
53 var _ *float64 = new[float64]()
54
55
56 func foo[A, B, C any](a A, b []B, c *C) B {
57
58 return b[0]
59 }
60
61
62 var s = foo[int, string, float64](1, []string{"first"}, new[float64]())
63
64
65 var _ float64 = foo(42, []float64{1.0}, &s)
66
67
68
69 func variadic[A, B any](A, B, ...B) int { panic(0) }
70
71
72 var _ = variadic(1, 2.3)
73 var _ = variadic(1, 2.3, 3.4, 4.5)
74 var _ = variadic[int, float64](1, 2.3, 3.4, 4)
75
76
77
78 func f1[T any](x T) {
79 f1(x)
80 }
81
82 func f2a[T any](x, y T) {
83 f2a(x, y)
84 }
85
86 func f2b[T any](x, y T) {
87 f2b(y, x)
88 }
89
90 func g2a[P, Q any](x P, y Q) {
91 g2a(x, y)
92 }
93
94 func g2b[P, Q any](x P, y Q) {
95 g2b(y, x)
96 }
97
98
99
100
101 func max[T interface{ ~int }](x ...T) T {
102 var x0 T
103 if len(x) > 0 {
104 x0 = x[0]
105 }
106 if len(x) > 1 {
107 x1 := max(x[1:]...)
108 if x1 > x0 {
109 return x1
110 }
111 }
112 return x0
113 }
114
115
116
117
118
119
120
121 func fboth[T any](chan T) {}
122 func frecv[T any](<-chan T) {}
123 func fsend[T any](chan<- T) {}
124
125 func _() {
126 var both chan int
127 var recv <-chan int
128 var send chan<-int
129
130 fboth(both)
131 fboth(recv )
132 fboth(send )
133
134 frecv(both)
135 frecv(recv)
136 frecv(send )
137
138 fsend(both)
139 fsend(recv )
140 fsend(send)
141 }
142
143 func ffboth[T any](func(chan T)) {}
144 func ffrecv[T any](func(<-chan T)) {}
145 func ffsend[T any](func(chan<- T)) {}
146
147 func _() {
148 var both func(chan int)
149 var recv func(<-chan int)
150 var send func(chan<- int)
151
152 ffboth(both)
153 ffboth(recv )
154 ffboth(send )
155
156 ffrecv(both )
157 ffrecv(recv)
158 ffrecv(send )
159
160 ffsend(both )
161 ffsend(recv )
162 ffsend(send)
163 }
164
165
166
167
168
169
170
171
172 func g1[T any]([]T) {}
173 func g2[T any]([]T, T) {}
174 func g3[T any](*T, ...T) {}
175
176 func _() {
177 type intSlice []int
178 g1([]int{})
179 g1(intSlice{})
180 g2(nil, 0)
181
182 type myString string
183 var s1 string
184 g3(nil, "1", myString("2"), "3")
185 g3(& s1, "1", myString("2"), "3")
186 _ = s1
187
188 type myStruct struct{x int}
189 var s2 myStruct
190 g3(nil, struct{x int}{}, myStruct{})
191 g3(&s2, struct{x int}{}, myStruct{})
192 g3(nil, myStruct{}, struct{x int}{})
193 g3(&s2, myStruct{}, struct{x int}{})
194 }
195
196
197
198 func append[T any](s []T, t ...T) []T { panic(0) }
199
200 func _() {
201 var f func()
202 type Funcs []func()
203 var funcs Funcs
204 _ = append(funcs, f)
205 }
206
207
208
209
210
211 func h[] () {}
212
213 func _() {
214 h [] ()
215 }
216
217
218
219 func _ [P any]()
220
View as plain text