Skip to content

Commit a938fc8

Browse files
Merge pull request #9217 from dotty-staging/add-tuple-types
Add Tuple match types
2 parents a3543f2 + 07c5ba1 commit a938fc8

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

library/src/scala/Tuple.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,23 @@ object Tuple {
103103
case x *: xs => S[Size[xs]]
104104
}
105105

106+
/** Fold a tuple `(T1, ..., Tn)` into `F[T1, F[... F[Tn, Z]...]]]` */
107+
type Fold[T <: Tuple, Z, F[_, _]] = T match
108+
case EmptyTuple => Z
109+
case h *: t => F[h, Fold[t, Z, F]]
110+
106111
/** Converts a tuple `(T1, ..., Tn)` to `(F[T1], ..., F[Tn])` */
107112
type Map[Tup <: Tuple, F[_]] <: Tuple = Tup match {
108113
case EmptyTuple => EmptyTuple
109114
case h *: t => F[h] *: Map[t, F]
110115
}
111116

117+
/** Converts a tuple `(T1, ..., Tn)` to a flattened `(..F[T1], ..., ..F[Tn])` */
118+
type FlatMap[Tup <: Tuple, F[_] <: Tuple] <: Tuple = Tup match {
119+
case EmptyTuple => EmptyTuple
120+
case h *: t => Concat[F[h], FlatMap[t, F]]
121+
}
122+
112123
/** Given two tuples, `A1 *: ... *: An * At` and `B1 *: ... *: Bn *: Bt`
113124
* where at least one of `At` or `Bt` is `EmptyTuple` or `Tuple`,
114125
* returns the tuple type `(A1, B1) *: ... *: (An, Bn) *: Ct`

tests/pos/tuple-flatmap.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
type Empty[X] = EmptyTuple
3+
type Twice[X] = (X, X)
4+
5+
def test =
6+
val a1: EmptyTuple = ??? : Tuple.FlatMap[EmptyTuple, Empty]
7+
val a2: EmptyTuple = ??? : Tuple.FlatMap[(Int, String), Empty]
8+
9+
val b1: EmptyTuple = ??? : Tuple.FlatMap[EmptyTuple, Tuple1]
10+
val b2: (Int, String) = ??? : Tuple.FlatMap[(Int, String), Tuple1]
11+
12+
val c1: EmptyTuple = ??? : Tuple.FlatMap[EmptyTuple, Twice]
13+
val c2: (Int, Int, String, String) = ??? : Tuple.FlatMap[(Int, String), Twice]
14+
val c3: (Int, List[Int], String, List[String]) = ??? : Tuple.FlatMap[(Int, String), [X] =>> (X, List[X])]

tests/pos/tuple-fold.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
type Empty[X] = EmptyTuple
3+
type Twice[X] = (X, X)
4+
5+
def test =
6+
val a1: EmptyTuple = ??? : Tuple.Fold[EmptyTuple, Nothing, Tuple2]
7+
val a2: (Int, (String, Nothing)) = ??? : Tuple.Fold[(Int, String), Nothing, Tuple2]
8+
val a3: Int | String | Char = ??? : Tuple.Fold[(Int, String, Char), Nothing, [X, Y] =>> X | Y]

0 commit comments

Comments
 (0)