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

Add an infix shorthand for Tuple.{Append, Concat} #21288

Merged
merged 2 commits into from
Nov 11, 2024
Merged
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
14 changes: 10 additions & 4 deletions library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ sealed trait Tuple extends Product {
runtime.Tuples.toIArray(this)

/** Return a copy of `this` tuple with an element appended */
inline def :* [This >: this.type <: Tuple, L] (x: L): Append[This, L] =
runtime.Tuples.append(x, this).asInstanceOf[Append[This, L]]
inline def :* [This >: this.type <: Tuple, L] (x: L): This :* L =
runtime.Tuples.append(x, this).asInstanceOf[This :* L]

/** Return a new tuple by prepending the element to `this` tuple.
* This operation is O(this.size)
Expand All @@ -34,8 +34,8 @@ sealed trait Tuple extends Product {
/** Return a new tuple by concatenating `this` tuple with `that` tuple.
* This operation is O(this.size + that.size)
*/
inline def ++ [This >: this.type <: Tuple](that: Tuple): Concat[This, that.type] =
runtime.Tuples.concat(this, that).asInstanceOf[Concat[This, that.type]]
inline def ++ [This >: this.type <: Tuple](that: Tuple): This ++ that.type =
runtime.Tuples.concat(this, that).asInstanceOf[This ++ that.type]

/** Return the size (or arity) of the tuple */
inline def size[This >: this.type <: Tuple]: Size[This] =
Expand Down Expand Up @@ -94,6 +94,9 @@ object Tuple {
case x *: xs => x *: Append[xs, Y]
}

/** An infix shorthand for `Append[X, Y]` */
infix type :*[X <: Tuple, Y] = Append[X, Y]

/** Type of the head of a tuple */
type Head[X <: Tuple] = X match {
case x *: _ => x
Expand Down Expand Up @@ -123,6 +126,9 @@ object Tuple {
case x1 *: xs1 => x1 *: Concat[xs1, Y]
}

/** An infix shorthand for `Concat[X, Y]` */
infix type ++[X <: Tuple, +Y <: Tuple] = Concat[X, Y]

/** Type of the element at position N in the tuple X */
type Elem[X <: Tuple, N <: Int] = X match {
case x *: xs =>
Expand Down
Loading