@@ -5,7 +5,7 @@ import compiletime._
5
5
import compiletime .ops .int ._
6
6
7
7
/** Tuple of arbitrary arity */
8
- sealed trait Tuple extends Product {
8
+ sealed trait Tuple extends Product :
9
9
import Tuple ._
10
10
11
11
/** Create a copy of this tuple as an Array */
@@ -78,76 +78,64 @@ sealed trait Tuple extends Product {
78
78
*/
79
79
inline def splitAt [This >: this .type <: Tuple ](n : Int ): Split [This , n.type ] =
80
80
runtime.Tuples .splitAt(this , n).asInstanceOf [Split [This , n.type ]]
81
- }
82
81
83
- object Tuple {
82
+ object Tuple :
84
83
85
84
/** Type of a tuple with an element appended */
86
- type Append [X <: Tuple , Y ] <: NonEmptyTuple = X match {
85
+ type Append [X <: Tuple , Y ] <: NonEmptyTuple = X match
87
86
case EmptyTuple => Y *: EmptyTuple
88
87
case x *: xs => x *: Append [xs, Y ]
89
- }
90
88
91
89
/** Type of the head of a tuple */
92
- type Head [X <: NonEmptyTuple ] = X match {
90
+ type Head [X <: NonEmptyTuple ] = X match
93
91
case x *: _ => x
94
- }
95
92
96
93
/** Type of the initial part of the tuple without its last element */
97
- type Init [X <: Tuple ] <: Tuple = X match {
94
+ type Init [X <: Tuple ] <: Tuple = X match
98
95
case _ *: EmptyTuple => EmptyTuple
99
96
case x *: xs =>
100
97
x *: Init [xs]
101
- }
102
98
103
99
/** Type of the tail of a tuple */
104
- type Tail [X <: NonEmptyTuple ] <: Tuple = X match {
100
+ type Tail [X <: NonEmptyTuple ] <: Tuple = X match
105
101
case _ *: xs => xs
106
- }
107
102
108
103
/** Type of the last element of a tuple */
109
- type Last [X <: Tuple ] = X match {
104
+ type Last [X <: Tuple ] = X match
110
105
case x *: EmptyTuple => x
111
106
case _ *: xs => Last [xs]
112
- }
113
107
114
108
/** Type of the concatenation of two tuples */
115
- type Concat [X <: Tuple , + Y <: Tuple ] <: Tuple = X match {
109
+ type Concat [X <: Tuple , + Y <: Tuple ] <: Tuple = X match
116
110
case EmptyTuple => Y
117
111
case x1 *: xs1 => x1 *: Concat [xs1, Y ]
118
- }
119
112
120
113
/** Type of the element at position N in the tuple X */
121
- type Elem [X <: Tuple , N <: Int ] = X match {
114
+ type Elem [X <: Tuple , N <: Int ] = X match
122
115
case x *: xs =>
123
- N match {
116
+ N match
124
117
case 0 => x
125
118
case S [n1] => Elem [xs, n1]
126
- }
127
- }
128
119
129
120
/** Literal constant Int size of a tuple */
130
- type Size [X <: Tuple ] <: Int = X match {
121
+ type Size [X <: Tuple ] <: Int = X match
131
122
case EmptyTuple => 0
132
123
case x *: xs => S [Size [xs]]
133
- }
134
124
135
125
/** Fold a tuple `(T1, ..., Tn)` into `F[T1, F[... F[Tn, Z]...]]]` */
136
126
type Fold [Tup <: Tuple , Z , F [_, _]] = Tup match
137
127
case EmptyTuple => Z
138
128
case h *: t => F [h, Fold [t, Z , F ]]
139
129
140
130
/** Converts a tuple `(T1, ..., Tn)` to `(F[T1], ..., F[Tn])` */
141
- type Map [Tup <: Tuple , F [_ <: Union [Tup ]]] <: Tuple = Tup match {
131
+ type Map [Tup <: Tuple , F [_ <: Union [Tup ]]] <: Tuple = Tup match
142
132
case EmptyTuple => EmptyTuple
143
133
case h *: t => F [h] *: Map [t, F ]
144
- }
145
134
146
135
/** Converts a tuple `(T1, ..., Tn)` to a flattened `(..F[T1], ..., ..F[Tn])` */
147
- type FlatMap [Tup <: Tuple , F [_ <: Union [Tup ]] <: Tuple ] <: Tuple = Tup match {
136
+ type FlatMap [Tup <: Tuple , F [_ <: Union [Tup ]] <: Tuple ] <: Tuple = Tup match
148
137
case EmptyTuple => EmptyTuple
149
138
case h *: t => Concat [F [h], FlatMap [t, F ]]
150
- }
151
139
152
140
/** Filters out those members of the tuple for which the predicate `P` returns `false`.
153
141
* A predicate `P[X]` is a type that can be either `true` or `false`. For example:
@@ -160,31 +148,27 @@ object Tuple {
160
148
* ```
161
149
* @syntax markdown
162
150
*/
163
- type Filter [Tup <: Tuple , P [_] <: Boolean ] <: Tuple = Tup match {
151
+ type Filter [Tup <: Tuple , P [_] <: Boolean ] <: Tuple = Tup match
164
152
case EmptyTuple => EmptyTuple
165
- case h *: t => P [h] match {
166
- case true => h *: Filter [t, P ]
167
- case false => Filter [t, P ]
168
- }
169
- }
153
+ case h *: t => P [h] match
154
+ case true => h *: Filter [t, P ]
155
+ case false => Filter [t, P ]
170
156
171
157
/** Given two tuples, `A1 *: ... *: An * At` and `B1 *: ... *: Bn *: Bt`
172
158
* where at least one of `At` or `Bt` is `EmptyTuple` or `Tuple`,
173
159
* returns the tuple type `(A1, B1) *: ... *: (An, Bn) *: Ct`
174
160
* where `Ct` is `EmptyTuple` if `At` or `Bt` is `EmptyTuple`, otherwise `Ct` is `Tuple`.
175
161
*/
176
- type Zip [T1 <: Tuple , T2 <: Tuple ] <: Tuple = (T1 , T2 ) match {
162
+ type Zip [T1 <: Tuple , T2 <: Tuple ] <: Tuple = (T1 , T2 ) match
177
163
case (h1 *: t1, h2 *: t2) => (h1, h2) *: Zip [t1, t2]
178
164
case (EmptyTuple , _) => EmptyTuple
179
165
case (_, EmptyTuple ) => EmptyTuple
180
166
case _ => Tuple
181
- }
182
167
183
168
/** Converts a tuple `(F[T1], ..., F[Tn])` to `(T1, ... Tn)` */
184
- type InverseMap [X <: Tuple , F [_]] <: Tuple = X match {
169
+ type InverseMap [X <: Tuple , F [_]] <: Tuple = X match
185
170
case F [x] *: t => x *: InverseMap [t, F ]
186
171
case EmptyTuple => EmptyTuple
187
- }
188
172
189
173
/** Implicit evidence. IsMappedBy[F][X] is present in the implicit scope iff
190
174
* X is a tuple for which each element's type is constructed via `F`. E.g.
@@ -194,22 +178,18 @@ object Tuple {
194
178
type IsMappedBy [F [_]] = [X <: Tuple ] =>> X =:= Map [InverseMap [X , F ], F ]
195
179
196
180
/** Transforms a tuple `(T1, ..., Tn)` into `(T1, ..., Ti)`. */
197
- type Take [T <: Tuple , N <: Int ] <: Tuple = N match {
181
+ type Take [T <: Tuple , N <: Int ] <: Tuple = N match
198
182
case 0 => EmptyTuple
199
- case S [n1] => T match {
200
- case EmptyTuple => EmptyTuple
201
- case x *: xs => x *: Take [xs, n1]
202
- }
203
- }
183
+ case S [n1] => T match
184
+ case EmptyTuple => EmptyTuple
185
+ case x *: xs => x *: Take [xs, n1]
204
186
205
187
/** Transforms a tuple `(T1, ..., Tn)` into `(Ti+1, ..., Tn)`. */
206
- type Drop [T <: Tuple , N <: Int ] <: Tuple = N match {
188
+ type Drop [T <: Tuple , N <: Int ] <: Tuple = N match
207
189
case 0 => T
208
- case S [n1] => T match {
209
- case EmptyTuple => EmptyTuple
210
- case x *: xs => Drop [xs, n1]
211
- }
212
- }
190
+ case S [n1] => T match
191
+ case EmptyTuple => EmptyTuple
192
+ case x *: xs => Drop [xs, n1]
213
193
214
194
/** Splits a tuple (T1, ..., Tn) into a pair of two tuples `(T1, ..., Ti)` and
215
195
* `(Ti+1, ..., Tn)`.
@@ -231,23 +211,19 @@ object Tuple {
231
211
def unapply (x : EmptyTuple ): true = true
232
212
233
213
/** Convert an array into a tuple of unknown arity and types */
234
- def fromArray [T ](xs : Array [T ]): Tuple = {
235
- val xs2 = xs match {
214
+ def fromArray [T ](xs : Array [T ]): Tuple =
215
+ val xs2 = xs match
236
216
case xs : Array [Object ] => xs
237
217
case xs => xs.map(_.asInstanceOf [Object ])
238
- }
239
218
runtime.Tuples .fromArray(xs2)
240
- }
241
219
242
220
/** Convert an immutable array into a tuple of unknown arity and types */
243
- def fromIArray [T ](xs : IArray [T ]): Tuple = {
244
- val xs2 : IArray [Object ] = xs match {
221
+ def fromIArray [T ](xs : IArray [T ]): Tuple =
222
+ val xs2 : IArray [Object ] = xs match
245
223
case xs : IArray [Object ] @ unchecked => xs
246
224
case _ =>
247
225
xs.map(_.asInstanceOf [Object ])
248
- }
249
226
runtime.Tuples .fromIArray(xs2)
250
- }
251
227
252
228
/** Convert a Product into a tuple of unknown arity and types */
253
229
def fromProduct (product : Product ): Tuple =
@@ -260,18 +236,16 @@ object Tuple {
260
236
given canEqualTuple [H1 , T1 <: Tuple , H2 , T2 <: Tuple ](
261
237
using eqHead : CanEqual [H1 , H2 ], eqTail : CanEqual [T1 , T2 ]
262
238
): CanEqual [H1 *: T1 , H2 *: T2 ] = CanEqual .derived
263
- }
264
239
265
240
/** A tuple of 0 elements */
266
241
type EmptyTuple = EmptyTuple .type
267
242
268
243
/** A tuple of 0 elements. */
269
- case object EmptyTuple extends Tuple {
244
+ case object EmptyTuple extends Tuple :
270
245
override def toString (): String = " ()"
271
- }
272
246
273
247
/** Tuple of arbitrary non-zero arity */
274
- sealed trait NonEmptyTuple extends Tuple {
248
+ sealed trait NonEmptyTuple extends Tuple :
275
249
import Tuple ._
276
250
277
251
/** Get the i-th element of this tuple.
@@ -298,11 +272,9 @@ sealed trait NonEmptyTuple extends Tuple {
298
272
inline def tail [This >: this .type <: NonEmptyTuple ]: Tail [This ] =
299
273
runtime.Tuples .tail(this ).asInstanceOf [Tail [This ]]
300
274
301
- }
302
275
303
276
@ showAsInfix
304
277
sealed abstract class *: [+ H , + T <: Tuple ] extends NonEmptyTuple
305
278
306
- object *: {
279
+ object `*:` :
307
280
def unapply [H , T <: Tuple ](x : H *: T ): (H , T ) = (x.head, x.tail)
308
- }
0 commit comments