Skip to content

Commit 1f6fc97

Browse files
Add Union type to the Tuple. Make tuple.toList return the union of its members
1 parent 2561a64 commit 1f6fc97

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

library/src/scala/Tuple.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ sealed trait Tuple extends Product {
1212
scala.runtime.Tuple.toArray(this)
1313

1414
/** Create a copy this tuple as a List */
15-
inline def toList: List[Object] =
16-
toArray.toList
15+
inline def toList: List[Union[this.type]] =
16+
toArray.toList.asInstanceOf[List[Union[this.type]]]
1717

1818
/** Create a copy this tuple as an IArray */
1919
inline def toIArray: IArray[Object] =
@@ -172,6 +172,11 @@ object Tuple {
172172
*/
173173
type Split[T <: Tuple, N <: Int] = (Take[T, N], Drop[T, N])
174174

175+
/** Given a tuple `(T1, ..., Tn)`, returns a union of its
176+
* member types: `T1 | ... | Tn`.
177+
*/
178+
type Union[T <: Tuple] = Fold[T, Nothing, [x, y] =>> x | y]
179+
175180
/** Empty tuple */
176181
def apply(): EmptyTuple = EmptyTuple
177182

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)