@@ -54,10 +54,34 @@ package object quoted {
5454 implicit object ExprOps {
5555 def (x : T ) toExpr[T : Liftable ] given QuoteContext : Expr [T ] = the[Liftable [T ]].toExpr(x)
5656
57- def (list : List [Expr [T ]]) toExprOfList[T : Type ] given QuoteContext : Expr [List [T ]] = list match {
58- case x :: xs => ' { $x :: $ {xs.toExprOfList} }
59- case Nil => ' { Nil }
57+ /** Lifts this sequence of expressions into an expression of a sequence
58+ *
59+ * Transforms a list of expression
60+ * `Seq(e1, e2, ...)` where `ei: Expr[T]`
61+ * to an expression equivalent to
62+ * `'{ Seq($e1, $e2, ...) }` typed as an `Expr[Seq[T]]`
63+ *
64+ * Usage:
65+ * ```scala
66+ * '{ List(${List(1, 2, 3).toExprOfSeq}: _*) } // equvalent to '{ List(1, 2, 3) }
67+ * ```
68+ */
69+ def (seq : Seq [Expr [T ]]) toExprOfSeq[T ] given (tp : Type [T ], qctx : QuoteContext ): Expr [Seq [T ]] = {
70+ import qctx .tasty ._
71+ Repeated (seq.map(_.unseal).toList, tp.unseal).seal.asInstanceOf [Expr [Seq [T ]]]
6072 }
73+
74+ /** Lifts this list of expressions into an expression of a list
75+ *
76+ * Transforms a list of expression
77+ * `List(e1, e2, ...)` where `ei: Expr[T]`
78+ * to an expression equivalent to
79+ * `'{ List($e1, $e2, ...) }` typed as an `Expr[List[T]]`
80+ */
81+ def (list : List [Expr [T ]]) toExprOfList[T ] given Type [T ], QuoteContext : Expr [List [T ]] =
82+ if (list.isEmpty) ' { Nil } else ' { List ($ {list.toExprOfSeq}: _* ) }
83+
84+
6185 }
6286
6387}
0 commit comments