Skip to content

Commit e1a2e61

Browse files
committed
Add NonEmptyTuple abstract class
... and move `_(_)`, `_.head`, `_.tail` to it. That way, we no not need to specify a specific (probably wildcarded) pair type when invoking these operations. I first tried to move the three operations simply to Tuple. But since `Unit` is a `Tuple` as well, this gives us funny error messages for `apply`. For instance this one in errMsgTests: ``` object Scope{ def foo(a: Int) = () foo(1)("2") } ``` If `Tuple` had an `apply` method, this would give `expected: Int, found : String`
1 parent fbb173f commit e1a2e61

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

library/src-scala3/scala/Tuple.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ object Tuple {
166166
}
167167
}
168168

169-
@showAsInfix
170-
sealed class *:[+H, +T <: Tuple] extends Tuple {
169+
abstract sealed class NonEmptyTuple extends Tuple {
171170
import Tuple._
172171

173172
rewrite def head: Any = {
@@ -253,6 +252,9 @@ sealed class *:[+H, +T <: Tuple] extends Tuple {
253252
}
254253
}
255254

255+
@showAsInfix
256+
sealed class *:[+H, +T <: Tuple] extends NonEmptyTuple
257+
256258
object *: {
257259
rewrite def unapply[H, T <: Tuple](x: H *: T) = (x.head, x.tail)
258260
}

tests/pos/matchtype.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,10 @@ object Test {
6464
checkSame[Concat[Unit, (String, Int)], (String, Int)]
6565
checkSame[Concat[(Boolean, Boolean), (String, Int)], Boolean *: Boolean *: (String, Int)]
6666
checkSub[(Boolean, Boolean, String, Int), Concat[(Boolean, Boolean), String *: Int *: Unit]]
67+
68+
rewrite def index[Xs <: NonEmptyTuple](xs: Xs, n: Int): Elem[Xs, n.type] = xs(n).asInstanceOf
69+
70+
val test = (1, "hi", true, 2.0)
71+
index(test, 0): Int
72+
index(test, 1): String
6773
}

0 commit comments

Comments
 (0)