1
1
package scala
2
2
import annotation .showAsInfix
3
3
import compiletime ._
4
- import scala . StagedTuple
4
+ import internal . _
5
5
6
6
sealed trait Tuple extends Any {
7
7
import Tuple ._
8
8
9
9
inline def toArray : Array [Object ] =
10
- if (stageIt) stagedToArray
10
+ if (stageIt) toArrayStaged
11
11
else inline constValueOpt[BoundedSize [this .type ]] match {
12
12
case Some (0 ) =>
13
- empty$Array
13
+ scala.runtime. DynamicTuple . empty$Array
14
14
case Some (1 ) =>
15
15
val t = asInstanceOf [Tuple1 [Object ]]
16
16
Array (t._1)
@@ -23,19 +23,19 @@ sealed trait Tuple extends Any {
23
23
case Some (4 ) =>
24
24
val t = asInstanceOf [Tuple4 [Object , Object , Object , Object ]]
25
25
Array (t._1, t._2, t._3, t._4)
26
- case Some (n) if n <= MaxSpecialized =>
26
+ case Some (n) if n <= scala.runtime. DynamicTuple . MaxSpecialized =>
27
27
to$Array (this , n)
28
28
case Some (n) =>
29
29
asInstanceOf [TupleXXL ].elems
30
30
case None =>
31
- dynamicToArray(this )
31
+ runtime. DynamicTuple . dynamicToArray(this )
32
32
}
33
33
34
- inline def stagedToArray : Array [Object ] =
34
+ inline def toArrayStaged : Array [Object ] =
35
35
$ { StagedTuple .toArrayStaged(' this , constValueOpt[BoundedSize [this .type ]]) }
36
36
37
37
inline def *: [H , This >: this .type <: Tuple ] (x : H ): H *: This =
38
- if (stageIt) stagedCons [H ](x)
38
+ if (stageIt) consStaged [H ](x)
39
39
else {
40
40
type Result = H *: This
41
41
inline constValueOpt[BoundedSize [this .type ]] match {
@@ -55,15 +55,15 @@ sealed trait Tuple extends Any {
55
55
case Some (n) =>
56
56
fromArray[H *: this .type ](cons$Array (x, toArray))
57
57
case _ =>
58
- dynamic_*: [This , H ](this , x)
58
+ runtime. DynamicTuple . dynamic_*: [This , H ](this , x)
59
59
}
60
60
}
61
61
62
- inline def stagedCons [H ] (x : H ): H *: this .type =
63
- $ { StagedTuple .stagedCons (' this , ' x , constValueOpt[BoundedSize [this .type ]]) }
62
+ inline def consStaged [H ] (x : H ): H *: this .type =
63
+ $ { StagedTuple .consStaged (' this , ' x , constValueOpt[BoundedSize [this .type ]]) }
64
64
65
65
inline def ++ [This >: this .type <: Tuple ](that : Tuple ): Concat [This , that.type ] =
66
- if (stageIt) stagedConcat (that).asInstanceOf
66
+ if (stageIt) concatStaged (that).asInstanceOf
67
67
else {
68
68
type Result = Concat [This , that.type ]
69
69
inline constValueOpt[BoundedSize [this .type ]] match {
@@ -99,35 +99,32 @@ sealed trait Tuple extends Any {
99
99
if (constValue[BoundedSize [that.type ]] == 0 ) this .asInstanceOf [Result ]
100
100
else genericConcat[Result ](this , that).asInstanceOf [Result ]
101
101
case None =>
102
- dynamic_++ [This , that.type ](this , that)
102
+ runtime. DynamicTuple . dynamic_++ [This , that.type ](this , that)
103
103
}
104
104
}
105
105
106
- inline def stagedConcat (that : Tuple ): Concat [this .type , that.type ] =
107
- $ { StagedTuple .stagedConcat (' this , constValueOpt[BoundedSize [this .type ]],
106
+ inline def concatStaged (that : Tuple ): Concat [this .type , that.type ] =
107
+ $ { StagedTuple .concatStaged (' this , constValueOpt[BoundedSize [this .type ]],
108
108
' that , constValueOpt[BoundedSize [that.type ]]) }
109
109
110
110
inline def genericConcat [T <: Tuple ](xs : Tuple , ys : Tuple ): Tuple =
111
111
fromArray[T ](xs.toArray ++ ys.toArray)
112
112
113
113
inline def size [This >: this .type <: Tuple ]: Size [This ] =
114
- if (stageIt) stagedSize .asInstanceOf
114
+ if (stageIt) sizeStaged .asInstanceOf
115
115
else {
116
116
type Result = Size [This ]
117
117
inline constValueOpt[BoundedSize [this .type ]] match {
118
118
case Some (n) => n.asInstanceOf [Result ]
119
- case _ => dynamicSize(this )
119
+ case _ => runtime. DynamicTuple . dynamicSize(this )
120
120
}
121
121
}
122
122
123
- inline def stagedSize : Size [this .type ] =
123
+ inline def sizeStaged : Size [this .type ] =
124
124
$ { StagedTuple .sizeStaged[Size [this .type ]](' this , constValueOpt[BoundedSize [this .type ]]) }
125
125
}
126
126
127
127
object Tuple {
128
- inline val MaxSpecialized = 22
129
- inline private val XXL = MaxSpecialized + 1
130
-
131
128
final val stageIt = false
132
129
133
130
type Head [X <: NonEmptyTuple ] = X match {
@@ -167,7 +164,7 @@ object Tuple {
167
164
168
165
private [scala] type BoundedSize [X ] = BoundedSizeRecur [X , 23 ]
169
166
170
- val empty$Array = Array [Object ]()
167
+ val $emptyArray = Array [Object ]()
171
168
172
169
def to$Array (xs : Tuple , n : Int ) = {
173
170
val arr = new Array [Object ](n)
@@ -188,7 +185,7 @@ object Tuple {
188
185
}
189
186
190
187
inline def fromArray [T <: Tuple ](xs : Array [Object ]): T =
191
- if (stageIt) stagedFromArray [T ](xs)
188
+ if (stageIt) fromArrayStaged [T ](xs)
192
189
else inline constValue[BoundedSize [T ]] match {
193
190
case 0 => ().asInstanceOf [T ]
194
191
case 1 => Tuple1 (xs(0 )).asInstanceOf [T ]
@@ -216,103 +213,15 @@ object Tuple {
216
213
case _ => TupleXXL (xs).asInstanceOf [T ]
217
214
}
218
215
219
- inline def stagedFromArray [T <: Tuple ](xs : Array [Object ]): T =
220
- $ { StagedTuple .fromArrayStaged[T ](' xs , constValueOpt[BoundedSize [this .type ]]) }
221
-
222
- def dynamicFromArray [T <: Tuple ](xs : Array [Object ]): T = xs.length match {
223
- case 0 => ().asInstanceOf [T ]
224
- case 1 => Tuple1 (xs(0 )).asInstanceOf [T ]
225
- case 2 => Tuple2 (xs(0 ), xs(1 )).asInstanceOf [T ]
226
- case 3 => Tuple3 (xs(0 ), xs(1 ), xs(2 )).asInstanceOf [T ]
227
- case 4 => Tuple4 (xs(0 ), xs(1 ), xs(2 ), xs(3 )).asInstanceOf [T ]
228
- case 5 => Tuple5 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 )).asInstanceOf [T ]
229
- case 6 => Tuple6 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 )).asInstanceOf [T ]
230
- case 7 => Tuple7 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 )).asInstanceOf [T ]
231
- case 8 => Tuple8 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 )).asInstanceOf [T ]
232
- case 9 => Tuple9 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 )).asInstanceOf [T ]
233
- case 10 => Tuple10 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 )).asInstanceOf [T ]
234
- case 11 => Tuple11 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 ), xs(10 )).asInstanceOf [T ]
235
- case 12 => Tuple12 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 ), xs(10 ), xs(11 )).asInstanceOf [T ]
236
- case 13 => Tuple13 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 ), xs(10 ), xs(11 ), xs(12 )).asInstanceOf [T ]
237
- case 14 => Tuple14 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 ), xs(10 ), xs(11 ), xs(12 ), xs(13 )).asInstanceOf [T ]
238
- case 15 => Tuple15 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 ), xs(10 ), xs(11 ), xs(12 ), xs(13 ), xs(14 )).asInstanceOf [T ]
239
- case 16 => Tuple16 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 ), xs(10 ), xs(11 ), xs(12 ), xs(13 ), xs(14 ), xs(15 )).asInstanceOf [T ]
240
- case 17 => Tuple17 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 ), xs(10 ), xs(11 ), xs(12 ), xs(13 ), xs(14 ), xs(15 ), xs(16 )).asInstanceOf [T ]
241
- case 18 => Tuple18 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 ), xs(10 ), xs(11 ), xs(12 ), xs(13 ), xs(14 ), xs(15 ), xs(16 ), xs(17 )).asInstanceOf [T ]
242
- case 19 => Tuple19 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 ), xs(10 ), xs(11 ), xs(12 ), xs(13 ), xs(14 ), xs(15 ), xs(16 ), xs(17 ), xs(18 )).asInstanceOf [T ]
243
- case 20 => Tuple20 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 ), xs(10 ), xs(11 ), xs(12 ), xs(13 ), xs(14 ), xs(15 ), xs(16 ), xs(17 ), xs(18 ), xs(19 )).asInstanceOf [T ]
244
- case 21 => Tuple21 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 ), xs(10 ), xs(11 ), xs(12 ), xs(13 ), xs(14 ), xs(15 ), xs(16 ), xs(17 ), xs(18 ), xs(19 ), xs(20 )).asInstanceOf [T ]
245
- case 22 => Tuple22 (xs(0 ), xs(1 ), xs(2 ), xs(3 ), xs(4 ), xs(5 ), xs(6 ), xs(7 ), xs(8 ), xs(9 ), xs(10 ), xs(11 ), xs(12 ), xs(13 ), xs(14 ), xs(15 ), xs(16 ), xs(17 ), xs(18 ), xs(19 ), xs(20 ), xs(21 )).asInstanceOf [T ]
246
- case _ => TupleXXL (xs).asInstanceOf [T ]
247
- }
248
-
249
- def dynamicToArray (self : Tuple ): Array [Object ] = (self : Any ) match {
250
- case self : Unit =>
251
- empty$Array
252
- case self : Tuple1 [_] =>
253
- val t = self.asInstanceOf [Tuple1 [Object ]]
254
- Array (t._1)
255
- case self : Tuple2 [_, _] =>
256
- val t = self.asInstanceOf [Tuple2 [Object , Object ]]
257
- Array (t._1, t._2)
258
- case self : Tuple3 [_, _, _] =>
259
- val t = self.asInstanceOf [Tuple3 [Object , Object , Object ]]
260
- Array (t._1, t._2, t._3)
261
- case self : Tuple4 [_, _, _, _] =>
262
- val t = self.asInstanceOf [Tuple4 [Object , Object , Object , Object ]]
263
- Array (t._1, t._2, t._3, t._4)
264
- case self : TupleXXL =>
265
- self.elems
266
- case self : Product =>
267
- val arr = new Array [Object ](self.productArity)
268
- for (i <- 0 until arr.length) arr(i) = self.productElement(i).asInstanceOf [Object ]
269
- arr
270
- }
271
-
272
- def dynamic_*: [This <: Tuple , H ] (self : Tuple , x : H ): H *: This = {
273
- type Result = H *: This
274
- (self : Any ) match {
275
- case Unit =>
276
- Tuple1 (x).asInstanceOf [Result ]
277
- case self : Tuple1 [_] =>
278
- Tuple2 (x, self._1).asInstanceOf [Result ]
279
- case self : Tuple2 [_, _] =>
280
- Tuple3 (x, self._1, self._2).asInstanceOf [Result ]
281
- case self : Tuple3 [_, _, _] =>
282
- Tuple4 (x, self._1, self._2, self._3).asInstanceOf [Result ]
283
- case self : Tuple4 [_, _, _, _] =>
284
- Tuple5 (x, self._1, self._2, self._3, self._4).asInstanceOf [Result ]
285
- case _ =>
286
- dynamicFromArray[Result ](cons$Array (x, dynamicToArray(self)))
287
- }
288
- }
289
-
290
- def dynamic_++ [This <: Tuple , That <: Tuple ](self : This , that : That ): Concat [This , That ] = {
291
- type Result = Concat [This , That ]
292
- (this : Any ) match {
293
- case self : Unit => return self.asInstanceOf [Result ]
294
- case _ =>
295
- }
296
- (that : Any ) match {
297
- case that : Unit => return self.asInstanceOf [Result ]
298
- case _ =>
299
- }
300
- dynamicFromArray[Result ](dynamicToArray(self) ++ dynamicToArray(that))
301
- }
302
-
303
- def dynamicSize [This <: Tuple ](self : This ): Size [This ] = (self : Any ) match {
304
- case self : Unit => 0 .asInstanceOf [Size [This ]]
305
- case self : TupleXXL => self.elems.length.asInstanceOf [Size [This ]]
306
- case self : Product => self.productArity.asInstanceOf [Size [This ]]
307
- }
216
+ inline def fromArrayStaged [T <: Tuple ](xs : Array [Object ]): T =
217
+ $ {StagedTuple .fromArrayStaged[T ](' xs , constValueOpt[BoundedSize [this .type ]])}
308
218
}
309
219
310
220
sealed trait NonEmptyTuple extends Tuple {
311
221
import Tuple ._
312
- import NonEmptyTuple ._
313
222
314
223
inline def head [This >: this .type <: NonEmptyTuple ]: Head [This ] =
315
- if (stageIt) stagedHead .asInstanceOf
224
+ if (stageIt) headStaged .asInstanceOf
316
225
else {
317
226
type Result = Head [This ]
318
227
val resVal = inline constValueOpt[BoundedSize [this .type ]] match {
@@ -328,22 +237,22 @@ sealed trait NonEmptyTuple extends Tuple {
328
237
case Some (4 ) =>
329
238
val t = asInstanceOf [Tuple4 [_, _, _, _]]
330
239
t._1
331
- case Some (n) if n > 4 && n <= MaxSpecialized =>
240
+ case Some (n) if n > 4 && n <= scala.runtime. DynamicTuple . MaxSpecialized =>
332
241
asInstanceOf [Product ].productElement(0 )
333
- case Some (n) if n > MaxSpecialized =>
242
+ case Some (n) if n > scala.runtime. DynamicTuple . MaxSpecialized =>
334
243
val t = asInstanceOf [TupleXXL ]
335
244
t.elems(0 )
336
245
case None =>
337
- dynamicHead[this .type ](this )
246
+ scala.runtime. DynamicTuple . dynamicHead[this .type ](this )
338
247
}
339
248
resVal.asInstanceOf [Result ]
340
249
}
341
250
342
- inline def stagedHead : Head [this .type ] =
251
+ inline def headStaged : Head [this .type ] =
343
252
$ { StagedTuple .headStaged[this .type ](' this , constValueOpt[BoundedSize [this .type ]]) }
344
253
345
254
inline def tail [This >: this .type <: NonEmptyTuple ]: Tail [This ] =
346
- if (stageIt) stagedTail .asInstanceOf
255
+ if (stageIt) tailStaged .asInstanceOf
347
256
else {
348
257
type Result = Tail [This ]
349
258
inline constValueOpt[BoundedSize [this .type ]] match {
@@ -364,21 +273,21 @@ sealed trait NonEmptyTuple extends Tuple {
364
273
case Some (n) if n > 5 =>
365
274
fromArray[Result ](toArray.tail)
366
275
case None =>
367
- dynamicTail[This ](this )
276
+ runtime. DynamicTuple . dynamicTail[This ](this )
368
277
}
369
278
}
370
279
371
- inline def stagedTail : Tail [this .type ] =
280
+ inline def tailStaged : Tail [this .type ] =
372
281
$ { StagedTuple .tailStaged[this .type ](' this , constValueOpt[BoundedSize [this .type ]]) }
373
282
374
283
inline def fallbackApply (n : Int ) =
375
284
inline constValueOpt[n.type ] match {
376
285
case Some (n : Int ) => error(" index out of bounds: " , n)
377
- case None => dynamicApply[this .type , n.type ](this , n)
286
+ case None => runtime. DynamicTuple . dynamicApply[this .type , n.type ](this , n)
378
287
}
379
288
380
289
inline def apply [This >: this .type <: NonEmptyTuple ](n : Int ): Elem [This , n.type ] =
381
- if (stageIt) stagedApply (n).asInstanceOf
290
+ if (stageIt) applyStaged (n).asInstanceOf
382
291
else {
383
292
type Result = Elem [This , n.type ]
384
293
inline constValueOpt[Size [this .type ]] match {
@@ -412,13 +321,13 @@ sealed trait NonEmptyTuple extends Tuple {
412
321
case Some (3 ) => t._4.asInstanceOf [Result ]
413
322
case _ => fallbackApply(n).asInstanceOf [Result ]
414
323
}
415
- case Some (s) if s > 4 && s <= MaxSpecialized =>
324
+ case Some (s) if s > 4 && s <= scala.runtime. DynamicTuple . MaxSpecialized =>
416
325
val t = asInstanceOf [Product ]
417
326
inline constValueOpt[n.type ] match {
418
327
case Some (n) if n >= 0 && n < s => t.productElement(n).asInstanceOf [Result ]
419
328
case _ => fallbackApply(n).asInstanceOf [Result ]
420
329
}
421
- case Some (s) if s > MaxSpecialized =>
330
+ case Some (s) if s > scala.runtime. DynamicTuple . MaxSpecialized =>
422
331
val t = asInstanceOf [TupleXXL ]
423
332
inline constValueOpt[n.type ] match {
424
333
case Some (n) if n >= 0 && n < s => t.elems(n).asInstanceOf [Result ]
@@ -428,53 +337,17 @@ sealed trait NonEmptyTuple extends Tuple {
428
337
}
429
338
}
430
339
431
- inline def stagedApply (n : Int ): Elem [this .type , n.type ] =
432
- $ { StagedTuple .applyStaged[this .type , n.type ](
433
- ' this , constValueOpt[Size [this .type ]],
434
- ' n , constValueOpt[n.type ]) }
435
- }
436
-
437
- object NonEmptyTuple {
438
- import Tuple ._
439
-
440
- def dynamicHead [This <: NonEmptyTuple ] (self : This ): Head [This ] = {
441
- type Result = Head [This ]
442
- val res = (self : Any ) match {
443
- case self : Tuple1 [_] => self._1
444
- case self : Tuple2 [_, _] => self._1
445
- case self : Tuple3 [_, _, _] => self._1
446
- case self : Tuple4 [_, _, _, _] => self._1
447
- case self : TupleXXL => self.elems(0 )
448
- case self : Product => self.productElement(0 )
449
- }
450
- res.asInstanceOf [Result ]
451
- }
452
-
453
- def dynamicTail [This <: NonEmptyTuple ] (self : This ): Tail [This ] = {
454
- type Result = Tail [This ]
455
- val res = (self : Any ) match {
456
- case self : Tuple1 [_] => ()
457
- case self : Tuple2 [_, _] => Tuple1 (self._2)
458
- case self : Tuple3 [_, _, _] => Tuple2 (self._2, self._3)
459
- case self : Tuple4 [_, _, _, _] => Tuple3 (self._2, self._3, self._4)
460
- case _ => dynamicFromArray[Result ](dynamicToArray(self).tail)
461
- }
462
- res.asInstanceOf [Result ]
463
- }
464
-
465
- def dynamicApply [This <: NonEmptyTuple , N <: Int ] (self : This , n : N ): Elem [This , N ] = {
466
- type Result = Elem [This , N ]
467
- val res = (self : Any ) match {
468
- case self : TupleXXL => self.elems(n)
469
- case self : Product => self.productElement(n)
470
- }
471
- res.asInstanceOf [Result ]
472
- }
340
+ inline def applyStaged (n : Int ): Elem [this .type , n.type ] =
341
+ $ {StagedTuple .applyStaged[this .type , n.type ](
342
+ ' this , constValueOpt[Size [this .type ]], ' n , constValueOpt[n.type ])}
473
343
}
474
344
475
345
@ showAsInfix
476
346
sealed class *: [+ H , + T <: Tuple ] extends NonEmptyTuple
477
347
478
348
object *: {
479
- inline def unapply [H , T <: Tuple ](x : H *: T ) = (x.head, x.tail)
349
+ inline def unapply [H , T <: Tuple ](x : H *: T ) =
350
+ // With stageIt on we cannot expand x.head in the same run and fails
351
+ if (Tuple .stageIt) (scala.runtime.DynamicTuple .dynamicHead(x), scala.runtime.DynamicTuple .dynamicTail(x))
352
+ else (x.head, x.tail)
480
353
}
0 commit comments