@@ -5,38 +5,51 @@ import internal._
5
5
6
6
import scala .runtime .DynamicTuple
7
7
8
+ /** Tuple of arbitrary arity */
8
9
sealed trait Tuple extends Any {
9
10
import Tuple ._
10
11
12
+ /** Create a copy this tuple as an Array */
11
13
inline def toArray : Array [Object ] =
12
14
DynamicTuple .dynamicToArray(this )
13
15
16
+ /** Return a new tuple by prepending the element to `this` tuple.
17
+ * This opteration is O(this.size)
18
+ */
14
19
inline def *: [H , This >: this .type <: Tuple ] (x : H ): H *: This =
15
20
DynamicTuple .dynamicCons[This , H ](this , x)
16
21
22
+ /** Return a new tuple by concatenating `this` tuple with `that` tuple.
23
+ * This opteration is O(this.size + that.size)
24
+ */
17
25
inline def ++ [This >: this .type <: Tuple ](that : Tuple ): Concat [This , that.type ] =
18
26
DynamicTuple .dynamicConcat[This , that.type ](this , that)
19
27
28
+ /** Return the size (or arity) of the tuple */
20
29
inline def size [This >: this .type <: Tuple ]: Size [This ] =
21
30
DynamicTuple .dynamicSize(this )
22
31
23
32
}
24
33
25
34
object Tuple {
26
35
36
+ /** Type of the head of a tuple */
27
37
type Head [X <: NonEmptyTuple ] = X match {
28
38
case x *: _ => x
29
39
}
30
40
41
+ /** Type of the tail of a tuple */
31
42
type Tail [X <: NonEmptyTuple ] <: Tuple = X match {
32
43
case _ *: xs => xs
33
44
}
34
45
46
+ /** Type of the concatenation of two tuples */
35
47
type Concat [X <: Tuple , + Y <: Tuple ] <: Tuple = X match {
36
48
case Unit => Y
37
49
case x1 *: xs1 => x1 *: Concat [xs1, Y ]
38
50
}
39
51
52
+ /** Type of the element a position N in the tuple X */
40
53
type Elem [X <: Tuple , N ] = X match {
41
54
case x *: xs =>
42
55
N match {
@@ -45,11 +58,13 @@ object Tuple {
45
58
}
46
59
}
47
60
61
+ /** Literal constant Int size of a tuple */
48
62
type Size [X ] <: Int = X match {
49
63
case Unit => 0
50
64
case x *: xs => S [Size [xs]]
51
65
}
52
66
67
+ /** Convert an array into a tuple of unknown arity and types */
53
68
def fromArray [T ](xs : Array [T ]): Tuple = {
54
69
val xs2 = xs match {
55
70
case xs : Array [Object ] => xs
@@ -58,20 +73,29 @@ object Tuple {
58
73
DynamicTuple .dynamicFromArray[Tuple ](xs2)
59
74
}
60
75
76
+ /** Convert a Product into a tuple of unknown arity and types */
61
77
def fromProduct (product : Product ): Tuple =
62
78
runtime.DynamicTuple .dynamicFromProduct[Tuple ](product)
63
79
64
80
}
65
81
82
+ /** Tuple of arbitrary non-zero arity */
66
83
sealed trait NonEmptyTuple extends Tuple {
67
84
import Tuple ._
68
85
86
+ /** Get the i-th element of this tuple.
87
+ * Equivalent to productElement but with a precise return type.
88
+ */
69
89
inline def apply [This >: this .type <: NonEmptyTuple ](n : Int ): Elem [This , n.type ] =
70
90
DynamicTuple .dynamicApply[This , n.type ](this , n)
71
91
92
+ /** Get the head of this tuple */
72
93
inline def head [This >: this .type <: NonEmptyTuple ]: Head [This ] =
73
94
DynamicTuple .dynamicApply[This , 0 ](this , 0 )
74
95
96
+ /** Get the tail of this tuple.
97
+ * This opteration is O(this.size)
98
+ */
75
99
inline def tail [This >: this .type <: NonEmptyTuple ]: Tail [This ] =
76
100
DynamicTuple .dynamicTail[This ](this )
77
101
0 commit comments