Skip to content

Commit 640de3c

Browse files
Merge pull request #9210 from dotty-staging/add-toList-to-tuples
Add toList method to Tuple
2 parents 57cfc9c + 2507194 commit 640de3c

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

library/src/scala/Tuple.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ sealed trait Tuple extends Product {
1111
inline def toArray: Array[Object] =
1212
scala.runtime.Tuple.toArray(this)
1313

14+
/** Create a copy this tuple as a List */
15+
inline def toList: List[Union[this.type]] =
16+
this.productIterator.toList
17+
.asInstanceOf[List[Union[this.type]]]
18+
1419
/** Create a copy this tuple as an IArray */
1520
inline def toIArray: IArray[Object] =
1621
scala.runtime.Tuple.toIArray(this)
@@ -168,6 +173,11 @@ object Tuple {
168173
*/
169174
type Split[T <: Tuple, N <: Int] = (Take[T, N], Drop[T, N])
170175

176+
/** Given a tuple `(T1, ..., Tn)`, returns a union of its
177+
* member types: `T1 | ... | Tn`. Returns `Nothing` if the tuple is empty.
178+
*/
179+
type Union[T <: Tuple] = Fold[T, Nothing, [x, y] =>> x | y]
180+
171181
/** Empty tuple */
172182
def apply(): EmptyTuple = EmptyTuple
173183

tests/run/toList.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
List(1, 2, 3)
2+
List(1, foo, 3)
3+
List(1, foo, 1)

tests/run/toList.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@main def Test =
2+
val l1: List[Int] = (1, 2, 3).toList
3+
val l2: List[Int | String] = (1, "foo", 3).toList
4+
val l3: List[Int | String | 1] = (1, "foo", 1).toList
5+
println(l1)
6+
println(l2)
7+
println(l3)

0 commit comments

Comments
 (0)