Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move NonEmptyTuple members into Tuple #21291

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions library/src/scala/NamedTuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ object NamedTuple:

export NamedTupleDecomposition.{
Names, DropNames,
apply, size, init, last, tail, take, drop, splitAt, ++, map, reverse, zip, toList, toArray, toIArray
apply, size, init, head, last, tail, take, drop, splitAt, ++, map, reverse, zip, toList, toArray, toIArray
}

extension [N <: Tuple, V <: Tuple](x: NamedTuple[N, V])
Expand All @@ -43,9 +43,6 @@ object NamedTuple:
// and should be reverted, just like NonEmptyList is also appealing at first, but a bad idea
// in the end.

/** The first element value of this tuple */
inline def head: Tuple.Elem[V, 0] = x.apply(0)

// inline def :* [L] (x: L): NamedTuple[Append[N, ???], Append[V, L] = ???
// inline def *: [H] (x: H): NamedTuple[??? *: N], H *: V] = ???

Expand Down Expand Up @@ -142,13 +139,14 @@ object NamedTupleDecomposition:
extension [N <: Tuple, V <: Tuple](x: NamedTuple[N, V])
/** The value (without the name) at index `n` of this tuple */
inline def apply(n: Int): Tuple.Elem[V, n.type] =
inline x.toTuple match
case tup: NonEmptyTuple => tup(n).asInstanceOf[Tuple.Elem[V, n.type]]
case tup => tup.productElement(n).asInstanceOf[Tuple.Elem[V, n.type]]
x.toTuple.apply(n).asInstanceOf[Tuple.Elem[V, n.type]]

/** The number of elements in this tuple */
inline def size: Tuple.Size[V] = x.toTuple.size

/** The first element value of this tuple */
inline def head: Tuple.Elem[V, 0] = apply(0)
EugeneFlesselle marked this conversation as resolved.
Show resolved Hide resolved

/** The last element value of this tuple */
inline def last: Tuple.Last[V] = apply(size - 1).asInstanceOf[Tuple.Last[V]]

Expand Down
52 changes: 25 additions & 27 deletions library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ sealed trait Tuple extends Product {
inline def *: [H, This >: this.type <: Tuple] (x: H): H *: This =
runtime.Tuples.cons(x, this).asInstanceOf[H *: This]

/** Get the i-th element of this tuple.
* Equivalent to productElement but with a precise return type.
*/
inline def apply[This >: this.type <: Tuple](n: Int): Elem[This, n.type] =
runtime.Tuples.apply(this, n).asInstanceOf[Elem[This, n.type]]

/** Get the head of this tuple */
inline def head[This >: this.type <: Tuple]: Head[This] =
runtime.Tuples.apply(this, 0).asInstanceOf[Head[This]]

/** Get the initial part of the tuple without its last element */
inline def init[This >: this.type <: Tuple]: Init[This] =
runtime.Tuples.init(this).asInstanceOf[Init[This]]

/** Get the last of this tuple */
inline def last[This >: this.type <: Tuple]: Last[This] =
runtime.Tuples.last(this).asInstanceOf[Last[This]]

/** Get the tail of this tuple.
* This operation is O(this.size)
*/
inline def tail[This >: this.type <: Tuple]: Tail[This] =
runtime.Tuples.tail(this).asInstanceOf[Tail[This]]

/** Return a new tuple by concatenating `this` tuple with `that` tuple.
* This operation is O(this.size + that.size)
*/
Expand Down Expand Up @@ -304,33 +328,7 @@ case object EmptyTuple extends Tuple {
}

/** Tuple of arbitrary non-zero arity */
sealed trait NonEmptyTuple extends Tuple {
import Tuple.*

/** Get the i-th element of this tuple.
* Equivalent to productElement but with a precise return type.
*/
inline def apply[This >: this.type <: NonEmptyTuple](n: Int): Elem[This, n.type] =
runtime.Tuples.apply(this, n).asInstanceOf[Elem[This, n.type]]

/** Get the head of this tuple */
inline def head[This >: this.type <: NonEmptyTuple]: Head[This] =
runtime.Tuples.apply(this, 0).asInstanceOf[Head[This]]

/** Get the initial part of the tuple without its last element */
inline def init[This >: this.type <: NonEmptyTuple]: Init[This] =
runtime.Tuples.init(this).asInstanceOf[Init[This]]

/** Get the last of this tuple */
inline def last[This >: this.type <: NonEmptyTuple]: Last[This] =
runtime.Tuples.last(this).asInstanceOf[Last[This]]

/** Get the tail of this tuple.
* This operation is O(this.size)
*/
inline def tail[This >: this.type <: NonEmptyTuple]: Tail[This] =
runtime.Tuples.tail(this).asInstanceOf[Tail[This]]
}
sealed trait NonEmptyTuple extends Tuple

@showAsInfix
sealed abstract class *:[+H, +T <: Tuple] extends NonEmptyTuple
Expand Down
8 changes: 4 additions & 4 deletions library/src/scala/runtime/Tuples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ object Tuples {
}
}

def tail(self: NonEmptyTuple): Tuple = (self: Any) match {
def tail(self: Tuple): Tuple = (self: Any) match {
case xxl: TupleXXL => xxlTail(xxl)
case _ => specialCaseTail(self)
}
Expand Down Expand Up @@ -558,16 +558,16 @@ object Tuples {
}
}

def init(self: NonEmptyTuple): Tuple = (self: Any) match {
def init(self: Tuple): Tuple = (self: Any) match {
case xxl: TupleXXL => xxlInit(xxl)
case _ => specialCaseInit(self)
}

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

def apply(self: NonEmptyTuple, n: Int): Any =
def apply(self: Tuple, n: Int): Any =
self.productElement(n)

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