Skip to content

Commit 232b3fa

Browse files
Merge pull request #6929 from dotty-staging/define-TupleXXL-using-IArray
Define TupleXXL and FunctionXXL using IArray
2 parents 746653e + 0c320fa commit 232b3fa

File tree

8 files changed

+51
-18
lines changed

8 files changed

+51
-18
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,6 @@ class Definitions {
710710
@threadUnsafe lazy val TupleXXLClass: ClassSymbolPerRun = perRunClass(ctx.requiredClassRef("scala.TupleXXL"))
711711
def TupleXXLModule(implicit ctx: Context): Symbol = TupleXXLClass.companionModule
712712

713-
def TupleXXL_apply(implicit ctx: Context): Symbol =
714-
TupleXXLModule.info.member(nme.apply).requiredSymbol("method", nme.apply, TupleXXLModule)(_.info.isVarArgsMethod)
715713
def TupleXXL_fromIterator(implicit ctx: Context): Symbol = TupleXXLModule.requiredMethod("fromIterator")
716714

717715
lazy val DynamicTupleModule: Symbol = ctx.requiredModule("scala.runtime.DynamicTuple")

library/src-bootstrapped/scala/IArray.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ object IArray {
4848
*/
4949
def empty[T: ClassTag]: IArray[T] = new Array[T](0).asInstanceOf
5050

51+
def emptyBooleanIArray = Array.emptyBooleanArray.asInstanceOf[IArray[Boolean]]
52+
def emptyByteIArray = Array.emptyByteArray.asInstanceOf[IArray[Byte]]
53+
def emptyCharIArray = Array.emptyCharArray.asInstanceOf[IArray[Char]]
54+
def emptyDoubleIArray = Array.emptyDoubleArray.asInstanceOf[IArray[Double]]
55+
def emptyFloatIArray = Array.emptyFloatArray.asInstanceOf[IArray[Float]]
56+
def emptyIntIArray = Array.emptyIntArray.asInstanceOf[IArray[Int]]
57+
def emptyLongIArray = Array.emptyLongArray.asInstanceOf[IArray[Long]]
58+
def emptyShortIArray = Array.emptyShortArray.asInstanceOf[IArray[Short]]
59+
def emptyObjectIArray = Array.emptyObjectArray.asInstanceOf[IArray[Object]]
60+
5161
/** An immutable array with given elements.
5262
*/
5363
def apply[T: ClassTag](xs: T*): IArray[T] = Array(xs: _*).asInstanceOf

library/src/scala/FunctionXXL.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package scala
33
/** A function with all parameters grouped in an array. */
44
trait FunctionXXL {
55

6-
def apply(xs: Array[Object]): Object
6+
def apply(xs: IArray[Object]): Object
77

88
override def toString() = "<functionXXL>"
99
}

library/src/scala/Tuple.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ sealed trait Tuple extends Any {
1313
inline def toArray: Array[Object] =
1414
DynamicTuple.dynamicToArray(this)
1515

16+
/** Create a copy this tuple as an IArray */
17+
inline def toIArray: IArray[Object] =
18+
DynamicTuple.dynamicToIArray(this)
19+
1620
/** Return a new tuple by prepending the element to `this` tuple.
1721
* This opteration is O(this.size)
1822
*/
@@ -79,6 +83,17 @@ object Tuple {
7983
DynamicTuple.dynamicFromArray[Tuple](xs2)
8084
}
8185

86+
/** Convert an immutable array into a tuple of unknown arity and types */
87+
def fromIArray[T](xs: IArray[T]): Tuple = {
88+
val xs2: IArray[Object] = xs match {
89+
case xs: IArray[Object] => xs
90+
case xs =>
91+
// TODO suport IArray.map
92+
xs.asInstanceOf[Array[T]].map(_.asInstanceOf[Object]).asInstanceOf[IArray[Object]]
93+
}
94+
DynamicTuple.dynamicFromIArray[Tuple](xs2)
95+
}
96+
8297
/** Convert a Product into a tuple of unknown arity and types */
8398
def fromProduct(product: Product): Tuple =
8499
runtime.DynamicTuple.dynamicFromProduct[Tuple](product)

library/src/scala/TupleXXL.scala

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
package scala
22
import java.util.Arrays.{deepEquals, deepHashCode}
33

4-
final class TupleXXL private (es: Array[Object]) extends Product {
4+
final class TupleXXL private (es: IArray[Object]) extends Product {
55
assert(es.length > 22)
66

77
def productElement(n: Int): Any = es(n)
88
def productArity: Int = es.length
99

10-
override def toString = elems.mkString("(", ",", ")")
11-
override def hashCode = getClass.hashCode * 41 + deepHashCode(elems)
10+
override def toString = elems.asInstanceOf[Array[Object]].mkString("(", ",", ")")
11+
override def hashCode = getClass.hashCode * 41 + deepHashCode(elems.asInstanceOf[Array[Object]])
1212
override def canEqual(that: Any): Boolean = that match {
1313
case that: TupleXXL => that.productArity == this.productArity
1414
case _ => false
1515
}
1616

1717
override def equals(that: Any) = that match {
18-
case that: TupleXXL => deepEquals(this.elems, that.elems)
18+
case that: TupleXXL => deepEquals(this.elems.asInstanceOf[Array[Object]], that.elems.asInstanceOf[Array[Object]])
1919
case _ => false
2020
}
21-
def elems: Array[Object] = es
21+
def elems: IArray[Object] = es
2222

2323
def tailXXL: TupleXXL = {
2424
assert(es.length > 23)
25-
new TupleXXL(es.tail)
25+
new TupleXXL(es.asInstanceOf[Array[Object]].tail.asInstanceOf[IArray[Object]]) // TODO use IArray.tail
2626
}
2727

28-
def toArray: Array[Object] = es.clone
28+
def toArray: Array[Object] = es.asInstanceOf[Array[Object]].clone // TODO use IArray.toArray
2929
}
3030
object TupleXXL {
31-
def fromIterator(elems: Iterator[Any]) = new TupleXXL(elems.map(_.asInstanceOf[Object]).toArray)
32-
def apply(elems: Array[Object]) = new TupleXXL(elems.clone)
33-
def apply(elems: Any*) = new TupleXXL(elems.asInstanceOf[Seq[Object]].toArray)
34-
def unapplySeq(x: TupleXXL): Option[Seq[Any]] = Some(x.elems.toSeq)
31+
def fromIterator(elems: Iterator[Any]) = new TupleXXL(elems.map(_.asInstanceOf[Object]).toArray.asInstanceOf[IArray[Object]]) // TODO use Iterator.toIArray
32+
def fromIArray(elems: IArray[Object]) = new TupleXXL(elems)
33+
def apply(elems: Any*) = new TupleXXL(IArray(elems.asInstanceOf[Seq[AnyRef]]: _*))
34+
def unapplySeq(x: TupleXXL): Option[Seq[Any]] = Some(x.elems.asInstanceOf[Array[Object]].toSeq) // TODO use IArray.toSeq
3535
}

library/src/scala/TupledFunction.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ package internal {
169169
def tupledFunctionXXL[F, G]: TupledFunction[F, G] = new TupledFunction {
170170
def tupled(f: F): G = ((args: TupleXXL) => f.asInstanceOf[FunctionXXL].apply(args.elems)).asInstanceOf[G]
171171
def untupled(g: G): F = new FunctionXXL {
172-
override def apply(xs: Array[Object]): AnyRef = g.asInstanceOf[TupleXXL => AnyRef].apply(TupleXXL(xs))
172+
override def apply(xs: IArray[Object]): AnyRef = g.asInstanceOf[TupleXXL => AnyRef].apply(TupleXXL.fromIArray(xs))
173173
}.asInstanceOf[F]
174174
}
175175

library/src/scala/runtime/DynamicTuple.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ object DynamicTuple {
5252
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]
5353
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]
5454
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]
55-
case _ => TupleXXL(xs).asInstanceOf[T]
55+
case _ => TupleXXL.fromIArray(xs.clone().asInstanceOf[IArray[Object]]).asInstanceOf[T]
5656
}
5757

58+
def dynamicFromIArray[T <: Tuple](xs: IArray[Object]): T =
59+
if (xs.length <= 22) dynamicFromArray(xs.asInstanceOf[Array[Object]])
60+
else TupleXXL.fromIArray(xs).asInstanceOf[T]
61+
5862
def dynamicFromProduct[T <: Tuple](xs: Product): T = (xs.productArity match {
5963
case 1 =>
6064
xs match {
@@ -169,7 +173,7 @@ object DynamicTuple {
169173
case _ =>
170174
xs match {
171175
case xs: TupleXXL => xs
172-
case xs => TupleXXL(xs.productIterator.map(_.asInstanceOf[Object]).toArray)
176+
case xs => TupleXXL.fromIArray(xs.productIterator.map(_.asInstanceOf[Object]).toArray.asInstanceOf[IArray[Object]]) // TODO use Iterator.toIArray
173177
}
174178
}).asInstanceOf[T]
175179

@@ -180,6 +184,12 @@ object DynamicTuple {
180184
case self: Product => productToArray(self)
181185
}
182186

187+
def dynamicToIArray(self: Tuple): IArray[Object] = (self: Any) match {
188+
case self: Unit => Array.emptyObjectArray.asInstanceOf[IArray[Object]] // TODO use IArray.emptyObjectIArray
189+
case self: TupleXXL => self.elems
190+
case self: Product => productToArray(self).asInstanceOf[IArray[Object]]
191+
}
192+
183193
def productToArray(self: Product): Array[Object] = {
184194
val arr = new Array[Object](self.productArity)
185195
for (i <- 0 until arr.length) arr(i) = self.productElement(i).asInstanceOf[Object]

tests/run-with-compiler/staged-tuples/StagedTuple.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object StagedTuple {
2828
case Some(n) if n <= MaxSpecialized =>
2929
'{to$Array($tup, ${ n.toExpr })}
3030
case Some(n) =>
31-
'{${ tup.as[TupleXXL] }.elems}
31+
'{ ${tup.as[TupleXXL]}.toArray }
3232
case None =>
3333
'{dynamicToArray($tup)}
3434
}

0 commit comments

Comments
 (0)