1
2
3
4
5 package conversions
6
7 import "unsafe"
8
9
10
11 func _[T ~byte]() T { return 255 }
12 func _[T ~byte]() T { return 256 }
13
14 func _[T ~byte]() {
15 const _ = T (0)
16 var _ T = 255
17 var _ T = 256
18 }
19
20 func _[T ~string]() T { return T('a') }
21 func _[T ~int | ~string]() T { return T('a') }
22 func _[T ~byte | ~int | ~string]() T { return T(256 ) }
23
24
25 func _[T ~string]() {
26 var _ string = 0
27 var _ T = 0
28 }
29
30
31 func _[
32 T1 any,
33 T2 interface{ m() },
34 T3 ~int | ~float64 | ~bool,
35 T4 ~int | ~string,
36 ]() {
37 _ = T1(0 )
38 _ = T2(1 )
39 _ = T3(2 )
40 _ = T4(3.14 )
41 }
42
43
44
45
46
47
48 func _[X ~int, T ~int](x X) T { return T(x) }
49 func _[X struct {
50 f int "foo"
51 }, T struct {
52 f int "bar"
53 }](x X) T {
54 return T(x)
55 }
56
57 type Foo struct {
58 f int "foo"
59 }
60 type Bar struct {
61 f int "bar"
62 }
63 type Far struct{ f float64 }
64
65 func _[X Foo, T Bar](x X) T { return T(x) }
66 func _[X Foo | Bar, T Bar](x X) T { return T(x) }
67 func _[X Foo, T Foo | Bar](x X) T { return T(x) }
68 func _[X Foo, T Far](x X) T {
69 return T(x )
70 }
71
72
73
74
75 func _[X ~*Foo, T ~*Bar](x X) T { return T(x) }
76 func _[X ~*Foo | ~*Bar, T ~*Bar](x X) T { return T(x) }
77 func _[X ~*Foo, T ~*Foo | ~*Bar](x X) T { return T(x) }
78 func _[X ~*Foo, T ~*Far](x X) T {
79 return T(x )
80 }
81
82
83
84 type (
85 B int
86 C int
87 X0 *B
88 T0 *C
89 )
90
91 func _(x X0) T0 { return T0(x ) }
92 func _[X X0, T T0](x X) T { return T(x ) }
93 func _[T T0](x X0) T { return T(x ) }
94 func _[X X0](x X) T0 { return T0(x ) }
95
96
97
98 func _[X Integer, T Integer](x X) T { return T(x) }
99 func _[X Unsigned, T Integer](x X) T { return T(x) }
100 func _[X Float, T Integer](x X) T { return T(x) }
101
102 func _[X Integer, T Unsigned](x X) T { return T(x) }
103 func _[X Unsigned, T Unsigned](x X) T { return T(x) }
104 func _[X Float, T Unsigned](x X) T { return T(x) }
105
106 func _[X Integer, T Float](x X) T { return T(x) }
107 func _[X Unsigned, T Float](x X) T { return T(x) }
108 func _[X Float, T Float](x X) T { return T(x) }
109
110 func _[X, T Integer | Unsigned | Float](x X) T { return T(x) }
111 func _[X, T Integer | ~string](x X) T {
112 return T(x )
113 }
114
115
116
117 func _[X, T Complex](x X) T { return T(x) }
118 func _[X, T Float | Complex](x X) T {
119 return T(x )
120 }
121
122
123
124 type myInt int
125 type myString string
126
127 func _[T ~string](x int) T { return T(x) }
128 func _[T ~string](x myInt) T { return T(x) }
129 func _[X Integer](x X) string { return string(x) }
130 func _[X Integer](x X) myString { return myString(x) }
131 func _[X Integer](x X) *string {
132 return (*string)(x )
133 }
134
135 func _[T ~string](x []byte) T { return T(x) }
136 func _[T ~string](x []rune) T { return T(x) }
137 func _[X ~[]byte, T ~string](x X) T { return T(x) }
138 func _[X ~[]rune, T ~string](x X) T { return T(x) }
139 func _[X Integer | ~[]byte | ~[]rune, T ~string](x X) T { return T(x) }
140 func _[X Integer | ~[]byte | ~[]rune, T ~*string](x X) T {
141 return T(x )
142 }
143
144
145
146 func _[T ~[]byte](x string) T { return T(x) }
147 func _[T ~[]rune](x string) T { return T(x) }
148 func _[T ~[]rune](x *string) T {
149 return T(x )
150 }
151
152 func _[X ~string, T ~[]byte](x X) T { return T(x) }
153 func _[X ~string, T ~[]rune](x X) T { return T(x) }
154 func _[X ~string, T ~[]byte | ~[]rune](x X) T { return T(x) }
155 func _[X ~*string, T ~[]byte | ~[]rune](x X) T {
156 return T(x )
157 }
158
159
160
161
162 type myUintptr uintptr
163
164 func _[X ~uintptr](x X) unsafe.Pointer { return unsafe.Pointer(x) }
165 func _[T unsafe.Pointer](x myUintptr) T { return T(x) }
166 func _[T unsafe.Pointer](x int64) T {
167 return T(x )
168 }
169
170
171
172 func _[T ~uintptr](x unsafe.Pointer) T { return T(x) }
173 func _[X unsafe.Pointer](x X) uintptr { return uintptr(x) }
174 func _[X unsafe.Pointer](x X) myUintptr { return myUintptr(x) }
175 func _[X unsafe.Pointer](x X) int64 {
176 return int64(x )
177 }
178
179
180
181
182 func _[X ~[]E, T ~[10]E, E any](x X) T { return T(x) }
183 func _[X ~[]E, T ~*[10]E, E any](x X) T { return T(x) }
184
185
186
187
188
189
190 type Signed interface {
191 ~int | ~int8 | ~int16 | ~int32 | ~int64
192 }
193
194 type Unsigned interface {
195 ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
196 }
197
198 type Integer interface {
199 Signed | Unsigned
200 }
201
202 type Float interface {
203 ~float32 | ~float64
204 }
205
206 type Complex interface {
207 ~complex64 | ~complex128
208 }
209
View as plain text