|
@@ -71,4 +71,87 @@ type StringableSignedInteger interface {
|
71
|
71
|
~int | ~int8 | ~int16 | ~int32 | ~int64
|
72
|
72
|
String() string
|
73
|
73
|
}
|
74
|
|
-// StringableSignedInteger OMIT
|
|
74
|
+// StringableSignedInteger OMIT
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+// COMPOSITE TYPE OMIT
|
|
78
|
+type byteseq interface{
|
|
79
|
+ string | []byte
|
|
80
|
+}
|
|
81
|
+
|
|
82
|
+func Join[T byteseq](a []T, sep T) (ret T) {
|
|
83
|
+ // some checks...
|
|
84
|
+
|
|
85
|
+ n := len(sep) * (len(a) - 1)
|
|
86
|
+ for _, v := range a {
|
|
87
|
+ n += len(v)
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ b := make([]byte, n)
|
|
91
|
+
|
|
92
|
+ bp := copy(b, a[0])
|
|
93
|
+ for _, s := range a[1:] {
|
|
94
|
+ bp += copy(b[bp:], sep)
|
|
95
|
+ bp += copy(b[bp:], s)
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ return T(b)
|
|
99
|
+}
|
|
100
|
+// COMPOSITE TYPE OMIT
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+// COMPOSITE STRUCT FIELD OMIT
|
|
104
|
+// structField is a type constraint whose type set consists of some
|
|
105
|
+// struct types that all have a field named x.
|
|
106
|
+type structField interface {
|
|
107
|
+ struct { a int; x int } |
|
|
108
|
+ struct { b int; x float64 } |
|
|
109
|
+ struct { c int; x uint64 }
|
|
110
|
+}
|
|
111
|
+
|
|
112
|
+// This function is INVALID.
|
|
113
|
+func IncrementX[T structField](p *T) {
|
|
114
|
+ v := p.x // INVALID: type of p.x is not the same for all types in set
|
|
115
|
+ v++
|
|
116
|
+ p.x = v
|
|
117
|
+}
|
|
118
|
+// COMPOSITE STRUCT FIELD OMIT
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+// TYPE CONVERSION OMIT
|
|
123
|
+type integer interface {
|
|
124
|
+ ~int | ~int8 | ~int16 | ~int32 | ~int64 |
|
|
125
|
+ ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
|
|
126
|
+}
|
|
127
|
+
|
|
128
|
+func Convert[To, From integer](from From) To {
|
|
129
|
+ to := To(from)
|
|
130
|
+ if From(to) != from {
|
|
131
|
+ panic("conversion out of range")
|
|
132
|
+ }
|
|
133
|
+ return to
|
|
134
|
+}
|
|
135
|
+
|
|
136
|
+// TYPE CONVERSION OMIT
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+// UNTYPED CONSTANTS OMIT
|
|
140
|
+type integer interface {
|
|
141
|
+ ~int | ~int8 | ~int16 | ~int32 | ~int64 |
|
|
142
|
+ ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
|
|
143
|
+}
|
|
144
|
+
|
|
145
|
+func Add10[T integer](s []T) {
|
|
146
|
+ for i, v := range s {
|
|
147
|
+ s[i] = v + 10 // OK: 10 can convert to any integer type
|
|
148
|
+ }
|
|
149
|
+}
|
|
150
|
+
|
|
151
|
+// This function is INVALID.
|
|
152
|
+func Add1024[T integer](s []T) {
|
|
153
|
+ for i, v := range s {
|
|
154
|
+ s[i] = v + 1024 // INVALID: 1024 not permitted by int8/uint8
|
|
155
|
+ }
|
|
156
|
+}
|
|
157
|
+// UNTYPED CONSTANTS OMIT
|