Skip to content

Commit 216fc92

Browse files
committed
Improvements to tuples: Allow prefix slice in fromArray
1 parent 210fe99 commit 216fc92

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

library/src/scala/Tuple.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,22 +271,29 @@ object Tuple:
271271
def unapply(x: EmptyTuple): true = true
272272

273273
/** Convert an array into a tuple of unknown arity and types */
274-
def fromArray[T](xs: Array[T]): Tuple = {
274+
def fromArray[T](xs: Array[T]): Tuple =
275+
fromArray(xs, xs.length)
276+
277+
/** Convert the first `n` elements of an array into a tuple of unknown arity and types */
278+
def fromArray[T](xs: Array[T], n: Int): Tuple = {
275279
val xs2 = xs match {
276280
case xs: Array[Object] => xs
277281
case xs => xs.map(_.asInstanceOf[Object])
278282
}
279-
runtime.Tuples.fromArray(xs2)
283+
runtime.Tuples.fromArray(xs2, n)
280284
}
281285

282286
/** Convert an immutable array into a tuple of unknown arity and types */
283-
def fromIArray[T](xs: IArray[T]): Tuple = {
287+
def fromIArray[T](xs: IArray[T]): Tuple = fromIArray(xs, xs.length)
288+
289+
/** Convert the first `n` elements of an immutable array into a tuple of unknown arity and types */
290+
def fromIArray[T](xs: IArray[T], n: Int): Tuple = {
284291
val xs2: IArray[Object] = xs match {
285292
case xs: IArray[Object] @unchecked => xs
286293
case _ =>
287294
xs.map(_.asInstanceOf[Object])
288295
}
289-
runtime.Tuples.fromIArray(xs2)
296+
runtime.Tuples.fromIArray(xs2, n)
290297
}
291298

292299
/** Convert a Product into a tuple of unknown arity and types */

library/src/scala/runtime/Tuples.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object Tuples {
2828
arr
2929
}
3030

31-
def fromArray(xs: Array[Object]): Tuple = xs.length match {
31+
def fromArray(xs: Array[Object], n: Int): Tuple = n match {
3232
case 0 => EmptyTuple
3333
case 1 => Tuple1(xs(0))
3434
case 2 => Tuple2(xs(0), xs(1))
@@ -55,10 +55,15 @@ object Tuples {
5555
case _ => TupleXXL.fromIArray(xs.clone().asInstanceOf[IArray[Object]]).asInstanceOf[Tuple]
5656
}
5757

58-
def fromIArray(xs: IArray[Object]): Tuple =
59-
if (xs.length <= 22) fromArray(xs.asInstanceOf[Array[Object]])
58+
def fromArray(xs: Array[Object]): Tuple = fromArray(xs, xs.length)
59+
60+
def fromIArray(xs: IArray[Object], n: Int): Tuple =
61+
if n <= 22 || n != xs.length
62+
then fromArray(xs.asInstanceOf[Array[Object]], n)
6063
else TupleXXL.fromIArray(xs).asInstanceOf[Tuple]
6164

65+
def fromIArray(xs: IArray[Object]): Tuple = fromIArray(xs, xs.length)
66+
6267
def fromProduct(xs: Product): Tuple = (xs.productArity match {
6368
case 0 => EmptyTuple
6469
case 1 =>

0 commit comments

Comments
 (0)