Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions library/src/scala/Tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ sealed trait Tuple extends Product {
inline def toArray: Array[Object] =
scala.runtime.Tuple.toArray(this)

/** Create a copy this tuple as a List */
inline def toList: List[Union[this.type]] =
this.productIterator.toList
.asInstanceOf[List[Union[this.type]]]

/** Create a copy this tuple as an IArray */
inline def toIArray: IArray[Object] =
scala.runtime.Tuple.toIArray(this)
Expand Down Expand Up @@ -168,6 +173,11 @@ object Tuple {
*/
type Split[T <: Tuple, N <: Int] = (Take[T, N], Drop[T, N])

/** Given a tuple `(T1, ..., Tn)`, returns a union of its
* member types: `T1 | ... | Tn`. Returns `Nothing` if the tuple is empty.
*/
type Union[T <: Tuple] = Fold[T, Nothing, [x, y] =>> x | y]

/** Empty tuple */
def apply(): EmptyTuple = EmptyTuple

Expand Down
3 changes: 3 additions & 0 deletions tests/run/toList.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
List(1, 2, 3)
List(1, foo, 3)
List(1, foo, 1)
7 changes: 7 additions & 0 deletions tests/run/toList.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@main def Test =
val l1: List[Int] = (1, 2, 3).toList
val l2: List[Int | String] = (1, "foo", 3).toList
val l3: List[Int | String | 1] = (1, "foo", 1).toList
println(l1)
println(l2)
println(l3)