Skip to content

Commit de89441

Browse files
Add toExprOfTuple method for tuples
1 parent f2e471e commit de89441

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

compiler/test/dotc/pos-test-pickling.blacklist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ matchtype.scala
2424

2525
# Opaque type
2626
i5720.scala
27+
28+
# Tuples
29+
toexproftuple.scala

library/src-bootstrapped/scala/quoted/package.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,9 @@ package object quoted {
140140
}
141141
}
142142

143+
def (tup: T) toExprOfTuple[T <: Tuple] given (ctx: QuoteContext, t: Type[Tuple.ReverseMap[T, Expr]]): Expr[Tuple.ReverseMap[T, Expr]] = {
144+
import ctx.tasty._
145+
val res: Expr[Tuple] = tup.asInstanceOf[Product].productIterator.toSeq.asInstanceOf[Seq[Expr[_]]].toExprOfTuple
146+
res.cast[Tuple.ReverseMap[T, Expr]]
147+
}
143148
}

library/src/scala/Tuple.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ object Tuple {
7474
case h *: t => F[h] *: Map[t, F]
7575
}
7676

77+
/** Converts a tuple `(F[T1], ..., F[Tn])` to `(T1, ... Tn)` */
78+
type ReverseMap[X <: Tuple, F[_]] <: Tuple = X match {
79+
case F[x] *: t => x *: ReverseMap[t, F]
80+
case Unit => Unit
81+
}
82+
7783
/** Convert an array into a tuple of unknown arity and types */
7884
def fromArray[T](xs: Array[T]): Tuple = {
7985
val xs2 = xs match {

tests/neg/toexproftuple.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.quoted._, scala.deriving._
2+
import given scala.quoted._
3+
4+
inline def mcr: Any = ${mcrImpl}
5+
def mcrImpl given (ctx: QuoteContext): Expr[Any] = {
6+
val tpl: (Expr[1], Expr[2], Expr[3]) = ('{1}, '{2}, '{3})
7+
'{val res: (1, 3, 3) = ${tpl.toExprOfTuple}; res} // error
8+
}

tests/pos/toexproftuple.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.quoted._, scala.deriving._
2+
import given scala.quoted._
3+
4+
inline def mcr: Any = ${mcrImpl}
5+
def mcrImpl given (ctx: QuoteContext): Expr[Any] = {
6+
val tpl: (Expr[1], Expr[2], Expr[3]) = ('{1}, '{2}, '{3})
7+
'{val res: (1, 2, 3) = ${tpl.toExprOfTuple}; res}
8+
}

0 commit comments

Comments
 (0)