diff --git a/library/src/scala/Tuple.scala b/library/src/scala/Tuple.scala index ea1c9f35da98..7a2b63ea6c30 100644 --- a/library/src/scala/Tuple.scala +++ b/library/src/scala/Tuple.scala @@ -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) @@ -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 diff --git a/tests/run/toList.check b/tests/run/toList.check new file mode 100644 index 000000000000..9d4a63215820 --- /dev/null +++ b/tests/run/toList.check @@ -0,0 +1,3 @@ +List(1, 2, 3) +List(1, foo, 3) +List(1, foo, 1) diff --git a/tests/run/toList.scala b/tests/run/toList.scala new file mode 100644 index 000000000000..3c8316e9e8a6 --- /dev/null +++ b/tests/run/toList.scala @@ -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)