Skip to content

Commit b3455cf

Browse files
Move NonEmptyTuple methods into Tuple
This is for the same reason as we changed `type Head[X <: NonEmptyTuple] = ...` to `type Head[X <: Tuple] = ...` Also, this is no more unsafe than the other operations already defined for all tuples. `drop(1)` for example was always defined, even though `tail` wasn't.
1 parent 5da69b9 commit b3455cf

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

library/src/scala/Tuple.scala

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ sealed trait Tuple extends Product:
3131
inline def *: [H, This >: this.type <: Tuple](x: H): H *: This =
3232
runtime.Tuples.cons(x, this).asInstanceOf[H *: This]
3333

34+
/** Get the i-th element of this tuple.
35+
* Equivalent to productElement but with a precise return type.
36+
*/
37+
inline def apply[This >: this.type <: Tuple](n: Int): Elem[This, n.type] =
38+
runtime.Tuples.apply(this, n).asInstanceOf[Elem[This, n.type]]
39+
40+
/** Get the head of this tuple */
41+
inline def head[This >: this.type <: Tuple]: Head[This] =
42+
runtime.Tuples.apply(this, 0).asInstanceOf[Head[This]]
43+
44+
/** Get the initial part of the tuple without its last element */
45+
inline def init[This >: this.type <: Tuple]: Init[This] =
46+
runtime.Tuples.init(this).asInstanceOf[Init[This]]
47+
48+
/** Get the last of this tuple */
49+
inline def last[This >: this.type <: Tuple]: Last[This] =
50+
runtime.Tuples.last(this).asInstanceOf[Last[This]]
51+
52+
/** Get the tail of this tuple.
53+
* This operation is O(this.size)
54+
*/
55+
inline def tail[This >: this.type <: Tuple]: Tail[This] =
56+
runtime.Tuples.tail(this).asInstanceOf[Tail[This]]
57+
3458
/** Return a new tuple by concatenating `this` tuple with `that` tuple.
3559
* This operation is O(this.size + that.size)
3660
*/
@@ -375,33 +399,7 @@ case object EmptyTuple extends Tuple {
375399
}
376400

377401
/** Tuple of arbitrary non-zero arity */
378-
sealed trait NonEmptyTuple extends Tuple {
379-
import Tuple.*
380-
381-
/** Get the i-th element of this tuple.
382-
* Equivalent to productElement but with a precise return type.
383-
*/
384-
inline def apply[This >: this.type <: NonEmptyTuple](n: Int): Elem[This, n.type] =
385-
runtime.Tuples.apply(this, n).asInstanceOf[Elem[This, n.type]]
386-
387-
/** Get the head of this tuple */
388-
inline def head[This >: this.type <: NonEmptyTuple]: Head[This] =
389-
runtime.Tuples.apply(this, 0).asInstanceOf[Head[This]]
390-
391-
/** Get the initial part of the tuple without its last element */
392-
inline def init[This >: this.type <: NonEmptyTuple]: Init[This] =
393-
runtime.Tuples.init(this).asInstanceOf[Init[This]]
394-
395-
/** Get the last of this tuple */
396-
inline def last[This >: this.type <: NonEmptyTuple]: Last[This] =
397-
runtime.Tuples.last(this).asInstanceOf[Last[This]]
398-
399-
/** Get the tail of this tuple.
400-
* This operation is O(this.size)
401-
*/
402-
inline def tail[This >: this.type <: NonEmptyTuple]: Tail[This] =
403-
runtime.Tuples.tail(this).asInstanceOf[Tail[This]]
404-
}
402+
sealed trait NonEmptyTuple extends Tuple
405403

406404
@showAsInfix
407405
sealed abstract class *:[+H, +T <: Tuple] extends NonEmptyTuple

library/src/scala/runtime/Tuples.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ object Tuples {
357357
}
358358
}
359359

360-
def tail(self: NonEmptyTuple): Tuple = (self: Any) match {
360+
def tail(self: Tuple): Tuple = (self: Any) match {
361361
case xxl: TupleXXL => xxlTail(xxl)
362362
case _ => specialCaseTail(self)
363363
}
@@ -565,16 +565,16 @@ object Tuples {
565565
}
566566
}
567567

568-
def init(self: NonEmptyTuple): Tuple = (self: Any) match {
568+
def init(self: Tuple): Tuple = (self: Any) match {
569569
case xxl: TupleXXL => xxlInit(xxl)
570570
case _ => specialCaseInit(self)
571571
}
572572

573-
def last(self: NonEmptyTuple): Any = (self: Any) match {
573+
def last(self: Tuple): Any = (self: Any) match {
574574
case self: Product => self.productElement(self.productArity - 1)
575575
}
576576

577-
def apply(self: NonEmptyTuple, n: Int): Any =
577+
def apply(self: Tuple, n: Int): Any =
578578
self.productElement(n)
579579

580580
// Benchmarks showed that this is faster than doing (it1 zip it2).copyToArray(...)

0 commit comments

Comments
 (0)