Skip to content

Commit 1c30071

Browse files
Use consistent naming and drop remaining braces in tuple type ops
1 parent 93281d1 commit 1c30071

File tree

3 files changed

+34
-42
lines changed

3 files changed

+34
-42
lines changed

library/src/scala/Tuple.scala

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ sealed trait Tuple extends Product:
2222
runtime.Tuples.toIArray(this)
2323

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

2828
/** Return a new tuple by prepending the element to `this` tuple.
2929
* This operation is O(this.size)
3030
*/
31-
inline def *: [H, This >: this.type <: Tuple] (x: H): H *: This =
31+
inline def *: [H, This >: this.type <: Tuple](x: H): H *: This =
3232
runtime.Tuples.cons(x, this).asInstanceOf[H *: This]
3333

3434
/** Return a new tuple by concatenating `this` tuple with `that` tuple.
@@ -104,14 +104,13 @@ object Tuple:
104104
/** The size of a tuple, represented as a literal constant subtype of Int */
105105
type Size[X <: Tuple] <: Int = X match
106106
case EmptyTuple => 0
107-
case x *: xs => S[Size[xs]]
107+
case _ *: xs => S[Size[xs]]
108108

109109
/** The type of the element at position N in the tuple X */
110110
type Elem[X <: Tuple, N <: Int] = X match
111-
case x *: xs =>
112-
N match
113-
case 0 => x
114-
case S[n1] => Elem[xs, n1]
111+
case x *: xs => N match
112+
case 0 => x
113+
case S[n1] => Elem[xs, n1]
115114

116115
/** The type of the first element of a tuple */
117116
// Only bounded by `<: Tuple` not `<: NonEmptyTuple`
@@ -134,8 +133,7 @@ object Tuple:
134133
/** The type of the initial part of a tuple without its last element */
135134
type Init[X <: Tuple] <: Tuple = X match
136135
case _ *: EmptyTuple => EmptyTuple
137-
case x *: xs =>
138-
x *: Init[xs]
136+
case x *: xs => x *: Init[xs]
139137

140138
/** The type of the tuple consisting of the first `N` elements of `X`,
141139
* or all elements if `N` exceeds `Size[X]`.
@@ -149,27 +147,24 @@ object Tuple:
149147
/** The type of the tuple consisting of all elements of `X` except the first `N` ones,
150148
* or no elements if `N` exceeds `Size[X]`.
151149
*/
152-
type Drop[X <: Tuple, N <: Int] <: Tuple = N match {
150+
type Drop[X <: Tuple, N <: Int] <: Tuple = N match
153151
case 0 => X
154-
case S[n1] => X match {
152+
case S[n1] => X match
155153
case EmptyTuple => EmptyTuple
156-
case x *: xs => Drop[xs, n1]
157-
}
158-
}
154+
case _ *: xs => Drop[xs, n1]
159155

160156
/** The pair type `(Take(X, N), Drop[X, N]). */
161157
type Split[X <: Tuple, N <: Int] = (Take[X, N], Drop[X, N])
162158

163159
/** Type of a tuple with an element appended */
164-
type Append[X <: Tuple, Y] <: NonEmptyTuple = X match {
160+
type Append[X <: Tuple, Y] <: NonEmptyTuple = X match
165161
case EmptyTuple => Y *: EmptyTuple
166162
case x *: xs => x *: Append[xs, Y]
167-
}
168163

169164
/** Type of the concatenation of two tuples `X` and `Y` */
170165
type Concat[X <: Tuple, +Y <: Tuple] <: Tuple = X match
171166
case EmptyTuple => Y
172-
case x1 *: xs1 => x1 *: Concat[xs1, Y]
167+
case x *: xs => x *: Concat[xs, Y]
173168

174169
/** An infix shorthand for `Concat[X, Y]` */
175170
infix type ++[X <: Tuple, +Y <: Tuple] = Concat[X, Y]
@@ -179,27 +174,27 @@ object Tuple:
179174
*/
180175
type IndexOf[X <: Tuple, Y] <: Int = X match
181176
case Y *: _ => 0
182-
case x *: xs => S[IndexOf[xs, Y]]
177+
case _ *: xs => S[IndexOf[xs, Y]]
183178
case EmptyTuple => 0
184179

185180
/** Fold a tuple `(T1, ..., Tn)` into `F[T1, F[... F[Tn, Z]...]]]` */
186-
type Fold[Tup <: Tuple, Z, F[_, _]] = Tup match
181+
type Fold[X <: Tuple, Z, F[_, _]] = X match
187182
case EmptyTuple => Z
188-
case h *: t => F[h, Fold[t, Z, F]]
183+
case x *: xs => F[x, Fold[xs, Z, F]]
189184

190185
/** The type of tuple `X` mapped with the type-level function `F`.
191186
* If `X = (T1, ..., Ti)` then `Map[X, F] = `(F[T1], ..., F[Ti])`.
192187
*/
193-
type Map[Tup <: Tuple, F[_ <: Union[Tup]]] <: Tuple = Tup match
188+
type Map[X <: Tuple, F[_ <: Union[X]]] <: Tuple = X match
194189
case EmptyTuple => EmptyTuple
195-
case h *: t => F[h] *: Map[t, F]
190+
case x *: xs => F[x] *: Map[xs, F]
196191

197192
/** The type of tuple `X` flat-mapped with the type-level function `F`.
198193
* If `X = (T1, ..., Ti)` then `FlatMap[X, F] = `F[T1] ++ ... ++ F[Ti]`
199194
*/
200-
type FlatMap[Tup <: Tuple, F[_ <: Union[Tup]] <: Tuple] <: Tuple = Tup match
195+
type FlatMap[X <: Tuple, F[_ <: Union[X]] <: Tuple] <: Tuple = X match
201196
case EmptyTuple => EmptyTuple
202-
case h *: t => Concat[F[h], FlatMap[t, F]]
197+
case x *: xs => Concat[F[x], FlatMap[xs, F]]
203198
// TODO: implement term level analogue
204199

205200
/** The type of the tuple consisting of all elements of tuple `X` that have types
@@ -217,9 +212,9 @@ object Tuple:
217212
*/
218213
type Filter[X <: Tuple, P[_] <: Boolean] <: Tuple = X match
219214
case EmptyTuple => EmptyTuple
220-
case h *: t => P[h] match
221-
case true => h *: Filter[t, P]
222-
case false => Filter[t, P]
215+
case x *: xs => P[x] match
216+
case true => x *: Filter[xs, P]
217+
case false => Filter[xs, P]
223218

224219
/** A tuple consisting of those indices `N` of tuple `X` where the predicate `P`
225220
* is true for `Elem[X, N]`. Indices are type level values <: Int.
@@ -242,17 +237,16 @@ object Tuple:
242237
* ```
243238
* @syntax markdown
244239
*/
245-
type Zip[T1 <: Tuple, T2 <: Tuple] <: Tuple = (T1, T2) match
246-
case (h1 *: t1, h2 *: t2) => (h1, h2) *: Zip[t1, t2]
240+
type Zip[X <: Tuple, Y <: Tuple] <: Tuple = (X, Y) match
241+
case (x *: xs, y *: ys) => (x, y) *: Zip[xs, ys]
247242
case (EmptyTuple, _) => EmptyTuple
248243
case (_, EmptyTuple) => EmptyTuple
249244
case _ => Tuple
250245

251246
/** Converts a tuple `(F[T1], ..., F[Tn])` to `(T1, ... Tn)` */
252-
type InverseMap[X <: Tuple, F[_]] <: Tuple = X match {
253-
case F[x] *: t => x *: InverseMap[t, F]
247+
type InverseMap[X <: Tuple, F[_]] <: Tuple = X match
248+
case F[x] *: xs => x *: InverseMap[xs, F]
254249
case EmptyTuple => EmptyTuple
255-
}
256250

257251
/** Implicit evidence. IsMappedBy[F][X] is present in the implicit scope iff
258252
* X is a tuple for which each element's type is constructed via `F`. E.g.
@@ -280,18 +274,17 @@ object Tuple:
280274
*/
281275
type Contains[X <: Tuple, Y] <: Boolean = X match
282276
case Y *: _ => true
283-
case x *: xs => Contains[xs, Y]
277+
case _ *: xs => Contains[xs, Y]
284278
case EmptyTuple => false
285279

286280
/** A type level Boolean indicating whether the type `Y` contains
287281
* none of the elements of `X`.
288282
* @pre The elements of `X` and `Y` are assumed to be singleton types
289283
*/
290284
type Disjoint[X <: Tuple, Y <: Tuple] <: Boolean = X match
291-
case x *: xs =>
292-
Contains[Y, x] match
293-
case true => false
294-
case false => Disjoint[xs, Y]
285+
case x *: xs => Contains[Y, x] match
286+
case true => false
287+
case false => Disjoint[xs, Y]
295288
case EmptyTuple => true
296289

297290
/** Empty tuple */

tests/neg/print-tuple-union.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
| and cannot be shown to be disjoint from it either.
1414
| Therefore, reduction cannot advance to the remaining case
1515
|
16-
| case h *: t => h | Tuple.Fold[t, Nothing, [x, y] =>> x | y]
16+
| case x *: xs => x | Tuple.Fold[xs, Nothing, [x, y] =>> x | y]
1717
|
1818
| longer explanation available when compiling with `-explain`

tests/neg/wildcard-match.check

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@
8787
| trying to reduce shapeless.tuples.length[T2]
8888
| trying to reduce Tuple.Size[shapeless.tuples.to[T2]]
8989
| failed since selector shapeless.tuples.to[T2]
90-
| does not uniquely determine parameters x, xs in
91-
| case x *: xs => scala.compiletime.ops.int.S[Tuple.Size[xs]]
92-
| The computed bounds for the parameters are:
93-
| x <: Int
90+
| does not uniquely determine parameter xs in
91+
| case _ *: xs => scala.compiletime.ops.int.S[Tuple.Size[xs]]
92+
| The computed bounds for the parameter are:
9493
| xs <: (Int, Int)

0 commit comments

Comments
 (0)