Skip to content

Commit c462bc7

Browse files
toExprOfTuple method added
1 parent 0f4d3b2 commit c462bc7

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package scala.quoted
2+
3+
trait ToExprOfTuple[T] {
4+
type Repr <: Tuple
5+
def apply(t: T): given QuoteContext => Expr[Repr]
6+
}
7+
8+
object ToExprOfTuple {
9+
type Aux[T, R <: Tuple] = ToExprOfTuple[T] { type Repr = R }
10+
}
11+
12+
def (t: T) toExprOfTuple[T] given (tc: ToExprOfTuple[T], ctx: QuoteContext): Expr[tc.Repr] = tc(t)
13+
14+
given [T <: NonEmptyTuple, KA, TEOT <: Tuple] as ToExprOfTuple.Aux[T, KA *: TEOT] given (
15+
eqv: Tuple.Head[T] =:= Expr[KA],
16+
teot: ToExprOfTuple.Aux[Tuple.Tail[T], TEOT],
17+
tph: Type[KA],
18+
tpt: Type[TEOT]) = new ToExprOfTuple[T] {
19+
type Repr = KA *: TEOT
20+
def apply(t: T): given QuoteContext => Expr[Repr] = {
21+
val hd: Expr[KA] = eqv(t.head)
22+
val tl: Expr[TEOT] = teot(t.tail)
23+
'{$hd *: $tl}
24+
}
25+
}
26+
27+
given as ToExprOfTuple[Unit] {
28+
type Repr = Unit
29+
def apply(t: Unit) = Expr.unitExpr
30+
}

library/src/scala/quoted/package.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ package object quoted {
8080
*/
8181
def (list: List[Expr[T]]) toExprOfList[T] given Type[T], QuoteContext: Expr[List[T]] =
8282
if (list.isEmpty) '{ Nil } else '{ List(${list.toExprOfSeq}: _*) }
83-
84-
8583
}
8684

8785
}

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)