Skip to content

Commit 1f5e9d3

Browse files
committed
Move This into method arguments
1 parent e6a22a0 commit 1f5e9d3

File tree

6 files changed

+23
-23
lines changed

6 files changed

+23
-23
lines changed

Diff for: library/src-bootstrapped/scala/Tuple.scala

+16-16
Original file line numberDiff line numberDiff line change
@@ -101,50 +101,50 @@ object Tuple {
101101
*/
102102
def *:(tail: Tail): H *: Tail = runtime.Tuples.cons(x, tail).asInstanceOf[H *: Tail]
103103

104-
extension [This <: Tuple](tuple: This)
104+
extension (tuple: Tuple)
105105
/** Get the head of this tuple */
106-
def head: Head[This] & Head[tuple.type] =
106+
def head[This >: tuple.type <: Tuple]: Head[This] & Head[tuple.type] =
107107
runtime.Tuples.apply(tuple, 0).asInstanceOf[Head[This] & Head[tuple.type]]
108108

109109
/** Get the tail of this tuple.
110110
* This operation is O(tuple.size)
111111
*/
112-
def tail: Tail[This] & Tail[tuple.type] =
112+
def tail[This >: tuple.type <: Tuple]: Tail[This] & Tail[tuple.type] =
113113
runtime.Tuples.tail(tuple).asInstanceOf[Tail[This] & Tail[tuple.type]]
114114

115115
/** Return the size (or arity) of the tuple */
116-
def size: Size[This] & Size[tuple.type] =
116+
def size[This >: tuple.type <: Tuple]: Size[This] & Size[tuple.type] =
117117
runtime.Tuples.size(tuple).asInstanceOf[Size[This] & Size[tuple.type]]
118118

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

125125
/** Get the initial part of the tuple without its last element */
126-
def init: Init[This] & Init[tuple.type] =
126+
def init[This >: tuple.type <: Tuple]: Init[This] & Init[tuple.type] =
127127
runtime.Tuples.init(tuple).asInstanceOf[Init[This] & Init[tuple.type]]
128128

129129
/** Get the last of this tuple */
130-
def last: Last[This] & Last[tuple.type] =
130+
def last[This >: tuple.type <: Tuple]: Last[This] & Last[tuple.type] =
131131
runtime.Tuples.last(tuple).asInstanceOf[Last[This] & Last[tuple.type]]
132132

133133
/** Return a copy of `tuple` with an element appended */
134-
def :*[X] (x: X): Append[This, X] & Append[tuple.type, X] =
134+
def :*[This >: tuple.type <: Tuple, X] (x: X): Append[This, X] & Append[tuple.type, X] =
135135
runtime.Tuples.append(x, tuple).asInstanceOf[Append[This, X] & Append[tuple.type, X]]
136136

137137
/** Return a new tuple by concatenating `this` tuple with `that` tuple.
138138
* This operation is O(this.size + that.size)
139139
*/
140-
def ++(that: Tuple): Concat[This, that.type] & Concat[tuple.type, that.type] =
140+
def ++[This >: tuple.type <: Tuple](that: Tuple): Concat[This, that.type] & Concat[tuple.type, that.type] =
141141
runtime.Tuples.concat(tuple, that).asInstanceOf[Concat[This, that.type] & Concat[tuple.type, that.type]]
142142

143143
/** Given a tuple `(a1, ..., am)`, returns the reversed tuple `(am, ..., a1)`
144144
* consisting all its elements.
145145
*/
146146
@experimental
147-
def reverse: Reverse[This] & Reverse[tuple.type] =
147+
def reverse[This >: tuple.type <: Tuple]: Reverse[This] & Reverse[tuple.type] =
148148
runtime.Tuples.reverse(tuple).asInstanceOf[Reverse[This] & Reverse[tuple.type]]
149149

150150
/** Given two tuples, `(a1, ..., an)` and `(a1, ..., an)`, returns a tuple
@@ -155,38 +155,38 @@ object Tuple {
155155
* `(A1, B1) *: ... *: (Ai, Bi) *: Tuple`
156156
*/
157157
// TODO change signature? def zip[That <: Tuple](that: That): Zip[This, tuple.type] & Zip[tuple.type, tuple.type] =
158-
def zip[That <: Tuple](that: That): Zip[This, That] & Zip[tuple.type, That] =
158+
def zip[This >: tuple.type <: Tuple, That <: Tuple](that: That): Zip[This, That] & Zip[tuple.type, That] =
159159
runtime.Tuples.zip(tuple, that).asInstanceOf[Zip[This, That] & Zip[tuple.type, That]]
160160

161161
/** Called on a tuple `(a1, ..., an)`, returns a new tuple `(f(a1), ..., f(an))`.
162162
* The result is typed as `(F[A1], ..., F[An])` if the tuple type is fully known.
163163
* If the tuple is of the form `a1 *: ... *: Tuple` (that is, the tail is not known
164164
* to be the cons type.
165165
*/
166-
def map[F[_]](f: [t] => t => F[t]): Map[This, F] & Map[tuple.type, F] =
166+
def map[This >: tuple.type <: Tuple, F[_]](f: [t] => t => F[t]): Map[This, F] & Map[tuple.type, F] =
167167
runtime.Tuples.map(tuple, f).asInstanceOf[Map[This, F] & Map[tuple.type, F]]
168168

169169
/** Given a tuple `(a1, ..., am)`, returns the tuple `(a1, ..., an)` consisting
170170
* of its first n elements.
171171
*/
172-
def take(n: Int): Take[This, n.type] & Take[tuple.type, n.type] =
172+
def take[This >: tuple.type <: Tuple](n: Int): Take[This, n.type] & Take[tuple.type, n.type] =
173173
runtime.Tuples.take(tuple, n).asInstanceOf[Take[This, n.type] & Take[tuple.type, n.type]]
174174

175175
/** Given a tuple `(a1, ..., am)`, returns the tuple `(an+1, ..., am)` consisting
176176
* all its elements except the first n ones.
177177
*/
178-
def drop(n: Int): Drop[This, n.type] & Take[tuple.type, n.type] =
178+
def drop[This >: tuple.type <: Tuple](n: Int): Drop[This, n.type] & Take[tuple.type, n.type] =
179179
runtime.Tuples.drop(tuple, n).asInstanceOf[Drop[This, n.type] & Take[tuple.type, n.type]]
180180

181181
/** Given a tuple `(a1, ..., am)`, returns a pair of the tuple `(a1, ..., an)`
182182
* consisting of the first n elements, and the tuple `(an+1, ..., am)` consisting
183183
* of the remaining elements.
184184
*/
185-
def splitAt(n: Int): Split[This, n.type] & Split[tuple.type, n.type] =
185+
def splitAt[This >: tuple.type <: Tuple](n: Int): Split[This, n.type] & Split[tuple.type, n.type] =
186186
runtime.Tuples.splitAt(tuple, n).asInstanceOf[Split[This, n.type] & Split[tuple.type, n.type]]
187187

188188
/** Create a copy of this tuple as a List */
189-
def toList: List[Union[This]] & List[Union[tuple.type]] =
189+
def toList[This >: tuple.type <: Tuple]: List[Union[This]] & List[Union[tuple.type]] =
190190
tuple.productIterator.toList.asInstanceOf[List[Union[This]] & List[Union[tuple.type]]]
191191
end extension
192192

Diff for: tests/neg/i13780-1.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Note that the code can be fixed with an explicit type argument to `.head`:
2020
*
2121
* def foo[VS <: Tuple](x: VS): SelectH[VS] = x match
22-
* case x: (h *: t) => Tuple.head[h *: t](x)
22+
* case x: (h *: t) => x.head[h *: t]
2323
*
2424
* So it *seems* like it would be fine to relax the rule, based on the insight
2525
* that `VS` in `Tuple.Head[VS & (h *: t)]` does not contribute anything to the
@@ -38,7 +38,7 @@ object ExampleFromSpata:
3838
case x: (h *: t) => x.head // error
3939

4040
def bar[VS <: Tuple](x: VS): SelectH[VS] = x match
41-
case x: (h *: t) => Tuple.head[h *: t](x) // ok
41+
case x: (h *: t) => x.head[h *: t] // ok
4242
end ExampleFromSpata
4343

4444
trait Z {

Diff for: tests/pos/i15743.gadt.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ class Alt:
99
case c1 @ C1() => // GADT constr: T := Tuple
1010
val t1: T = c1.getZ
1111
val t2: Int *: T = (1: Int) *: t1
12-
val i1: Int = (t2: Int *: T).head
12+
val i1: Int = (t2: Int *: T).head[Int *: T]

Diff for: tests/pos/i15743.pass.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// Like pos/i15743 but already passed, because the bounds are never lost so reduction never fails
22
class Pass:
33
def pass[T >: Tuple <: Tuple](t2: Int *: T) =
4-
val i1: Int = (t2: Int *: T).head
4+
val i1: Int = (t2: Int *: T).head[Int *: T]

Diff for: tests/pos/i15743.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ case class Bar[T <: Tuple](val x: Int *: T)
22

33
class Test:
44
def fail(e: Any): Int =
5-
e match { case b: Bar[t] => b.x.head }
5+
e match { case b: Bar[t] => b.x.head[Int *: t] }

Diff for: tests/run/tuple-ops.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ val r5: Unit = a.zip(d)
1313
// Map
1414
case class Foo[X](x: X)
1515

16-
val r6: (Int, Int, Int) = a.map[[t] =>> Int]([t] => (x: t) => x match {
16+
val r6: (Int, Int, Int) = a.map[(1, 2, 3), [t] =>> Int]([t] => (x: t) => x match {
1717
case x: Int => x * x
1818
case _ => ???
1919
})
2020

2121
val r7: ((1, Foo[1]), (2, Foo[2]), (3, Foo[3])) =
22-
a.map[[t] =>> (t, Foo[t])]( [t] => (x: t) => (x, Foo(x)) )
22+
a.map[(1, 2, 3), [t] =>> (t, Foo[t])]( [t] => (x: t) => (x, Foo(x)) )
2323

2424
// More Zip
2525
val t1: Int *: Long *: Tuple = (1, 2l, 100, 200)

0 commit comments

Comments
 (0)