Skip to content

Commit 68b0295

Browse files
Add toTuple method to Product
1 parent 2c12b93 commit 68b0295

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

library/src/dotty/DottyPredef.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ object DottyPredef {
3838

3939
inline def the[T] given (x: T): x.type = x
4040

41+
def (p: P) toTuple[P <: Product] given (m: scala.deriving.Mirror.ProductOf[P]): m.MirroredElemTypes =
42+
Tuple.fromArray(p.productIterator.toArray).asInstanceOf[m.MirroredElemTypes]
4143
}

tests/neg/product-to-tuple.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
case class Foo(x: Int, y: String)
2+
3+
val x = Foo(1, "2")
4+
val y: (Int, Boolean) = x.toTuple // error
5+
6+
case class Bar[I <: Int, S <: String](x: I, y: S)
7+
val a: Bar[1, "2"] = Bar(1, "2")
8+
val b: (1, "3") = a.toTuple // error

tests/pos/product-to-tuple.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
case class Foo(x: Int, y: String)
2+
3+
val x = Foo(1, "2")
4+
val y: (Int, String) = x.toTuple
5+
6+
case class Bar[I <: Int, S <: String](x: I, y: S)
7+
val a: Bar[1, "2"] = Bar(1, "2")
8+
val b: (1, "2") = a.toTuple

0 commit comments

Comments
 (0)