Skip to content

Commit 27fa4b1

Browse files
committed
Add quoted.Expr.{ofSeq, ofList}
Also * Let `ofList` receive a `Seq` * Remove `toExprOfSeq` and `toExprOfList`
1 parent 956e256 commit 27fa4b1

File tree

15 files changed

+46
-45
lines changed

15 files changed

+46
-45
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ object Liftable {
110110

111111
given [T: Type: Liftable] : Liftable[Seq[T]] = new Liftable[Seq[T]] {
112112
def toExpr(xs: Seq[T]): (given QuoteContext) => Expr[Seq[T]] =
113-
xs.map(summon[Liftable[T]].toExpr).toExprOfSeq
113+
Expr.ofSeq(xs.map(summon[Liftable[T]].toExpr))
114114
}
115115

116116
given [T: Type: Liftable] : Liftable[List[T]] = new Liftable[List[T]] {
117117
def toExpr(xs: List[T]): (given QuoteContext) => Expr[List[T]] =
118-
xs.map(summon[Liftable[T]].toExpr).toExprOfList
118+
Expr.ofList(xs.map(summon[Liftable[T]].toExpr))
119119
}
120120

121121
given [T: Type: Liftable] : Liftable[Set[T]] = new Liftable[Set[T]] {

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

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,6 @@ package object quoted {
99
implicit object ExprOps {
1010
def (x: T) toExpr[T: Liftable](given QuoteContext): Expr[T] = summon[Liftable[T]].toExpr(x)
1111

12-
/** Lifts this sequence of expressions into an expression of a sequence
13-
*
14-
* Transforms a sequence of expression
15-
* `Seq(e1, e2, ...)` where `ei: Expr[T]`
16-
* to an expression equivalent to
17-
* `'{ Seq($e1, $e2, ...) }` typed as an `Expr[Seq[T]]`
18-
*
19-
* Usage:
20-
* ```scala
21-
* '{ List(${List(1, 2, 3).toExprOfSeq}: _*) } // equvalent to '{ List(1, 2, 3) }
22-
* ```
23-
*/
24-
def (seq: Seq[Expr[T]]) toExprOfSeq[T](given tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] = {
25-
import qctx.tasty._
26-
Repeated(seq.map(_.unseal).toList, tp.unseal).seal.asInstanceOf[Expr[Seq[T]]]
27-
}
28-
29-
/** Lifts this list of expressions into an expression of a list
30-
*
31-
* Transforms a list of expression
32-
* `List(e1, e2, ...)` where `ei: Expr[T]`
33-
* to an expression equivalent to
34-
* `'{ List($e1, $e2, ...) }` typed as an `Expr[List[T]]`
35-
*/
36-
def (list: List[Expr[T]]) toExprOfList[T](given Type[T], QuoteContext): Expr[List[T]] =
37-
if (list.isEmpty) '{ Nil } else '{ List(${list.toExprOfSeq}: _*) }
38-
3912
/** Lifts this sequence of expressions into an expression of a tuple
4013
*
4114
* Transforms a sequence of expression
@@ -92,7 +65,7 @@ package object quoted {
9265
case Seq('{ $x1: $t1 }, '{ $x2: $t2 }, '{ $x3: $t3 }, '{ $x4: $t4 }, '{ $x5: $t5 }, '{ $x6: $t6 }, '{ $x7: $t7 }, '{ $x8: $t8 }, '{ $x9: $t9 }, '{ $x10: $t10 }, '{ $x11: $t11 }, '{ $x12: $t12 }, '{ $x13: $t13 }, '{ $x14: $t14 }, '{ $x15: $t15 }, '{ $x16: $t16 }, '{ $x17: $t17 }, '{ $x18: $t18 }, '{ $x19: $t19 }, '{ $x20: $t20 }, '{ $x21: $t21 }, '{ $x22: $t22 }) =>
9366
'{ Tuple22($x1, $x2, $x3, $x4, $x5, $x6, $x7, $x8, $x9, $x10, $x11, $x12, $x13, $x14, $x15, $x16, $x17, $x18, $x19, $x20, $x21, $x22) }
9467
case _ =>
95-
'{ Tuple.fromIArray(IArray(${seq.toExprOfSeq}: _*)) }
68+
'{ Tuple.fromIArray(IArray(${Expr.ofSeq(seq)}: _*)) }
9669
}
9770
}
9871

library/src/scala/quoted/Expr.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,34 @@ package quoted {
6565
Block(statements.map(_.unseal), expr.unseal).seal.asInstanceOf[Expr[T]]
6666
}
6767

68+
/** Lifts this sequence of expressions into an expression of a sequence
69+
*
70+
* Transforms a sequence of expression
71+
* `Seq(e1, e2, ...)` where `ei: Expr[T]`
72+
* to an expression equivalent to
73+
* `'{ Seq($e1, $e2, ...) }` typed as an `Expr[Seq[T]]`
74+
*
75+
* Usage:
76+
* ```scala
77+
* '{ List(${Expr.ofSeq(List(1, 2, 3))}: _*) } // equvalent to '{ List(1, 2, 3) }
78+
* ```
79+
*/
80+
def ofSeq[T](xs: Seq[Expr[T]])(given tp: Type[T], qctx: QuoteContext): Expr[Seq[T]] = {
81+
import qctx.tasty._
82+
Repeated(xs.map(_.unseal).toList, tp.unseal).seal.asInstanceOf[Expr[Seq[T]]]
83+
}
84+
85+
86+
/** Lifts this list of expressions into an expression of a list
87+
*
88+
* Transforms a list of expression
89+
* `List(e1, e2, ...)` where `ei: Expr[T]`
90+
* to an expression equivalent to
91+
* `'{ List($e1, $e2, ...) }` typed as an `Expr[List[T]]`
92+
*/
93+
def ofList[T](xs: Seq[Expr[T]])(given Type[T], QuoteContext): Expr[List[T]] =
94+
if (xs.isEmpty) '{ Nil } else '{ List(${ofSeq(xs)}: _*) }
95+
6896
}
6997

7098
}

tests/run-macros/f-interpolator-neg/Macros_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ object Macro {
6969
}
7070
val parts = parts0.map { case Const(s) => s }
7171
dotty.internal.StringContextMacro.interpolate(parts.toList, args.toList, argsExpr, reporter) // Discard result
72-
errors.result().toExprOfList
72+
Expr.ofList(errors.result())
7373
}
7474
}

tests/run-macros/i6765-b/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import scala.quoted.given
44
inline def foo = ${fooImpl}
55

66
def fooImpl(given qctx: QuoteContext) = {
7-
val res = List('{"One"}).toExprOfList
7+
val res = Expr.ofList(List('{"One"}))
88
res.show.toExpr
99
}

tests/run-macros/i6765-c/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import scala.quoted.given
44
inline def foo(inline n: Int) = ${fooImpl(n)}
55

66
def fooImpl(n: Int)(given qctx: QuoteContext) = {
7-
val res = List.tabulate(n)(i => ("#" + i).toExpr).toExprOfList
7+
val res = Expr.ofList(List.tabulate(n)(i => ("#" + i).toExpr))
88
'{ ${res.show.toExpr} + "\n" + $res.toString + "\n" }
99
}

tests/run-macros/i6765/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ inline def foo = ${fooImpl}
55

66
def fooImpl(given qctx: QuoteContext) = {
77
import qctx.tasty._
8-
val res = List('{"One"}).toExprOfList
8+
val res = Expr.ofList(List('{"One"}))
99
res.show.toExpr
1010
}

tests/run-macros/i7008/macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ def mcrProxy(expr: Expr[Boolean])(given QuoteContext): Expr[Unit] = {
1414

1515
def mcrImpl[T](func: Expr[Seq[Box[T]] => Unit], expr: Expr[T])(given ctx: QuoteContext, tt: Type[T]): Expr[Unit] = {
1616
import ctx.tasty._
17-
val arg = Seq('{(Box($expr))}).toExprOfSeq
17+
val arg = Expr.ofSeq(Seq('{(Box($expr))}))
1818
func(arg)
1919
}

tests/run-macros/quote-matcher-string-interpolator-3/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object Macros {
1010
self match {
1111
case '{ StringContext(${ConstSeq(parts)}: _*) } =>
1212
val upprerParts: List[String] = parts.toList.map(_.toUpperCase)
13-
val upprerPartsExpr: Expr[List[String]] = upprerParts.map(_.toExpr).toExprOfList
13+
val upprerPartsExpr: Expr[List[String]] = Expr.ofList(upprerParts.map(_.toExpr))
1414
'{ StringContext($upprerPartsExpr: _*).s($args: _*) }
1515
case _ =>
1616
'{

tests/run-macros/quote-matcher-string-interpolator/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macros {
99
private def impl(self: Expr[StringContext], args: Expr[Seq[String]])(given QuoteContext): Expr[String] = {
1010
self match {
1111
case '{ StringContext(${ExprSeq(parts)}: _*) } =>
12-
val parts2 = parts.map(x => '{ $x.reverse }).toList.toExprOfList
12+
val parts2 = Expr.ofList(parts.map(x => '{ $x.reverse }))
1313
'{ StringContext($parts2: _*).s($args: _*) }
1414
case _ =>
1515
'{ "ERROR" }

tests/run-macros/quote-toExprOfSeq/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import scala.quoted.given
44
inline def seq = ${fooImpl}
55

66
def fooImpl(given qctx: QuoteContext) = {
7-
List('{1}, '{2}, '{3}).toExprOfSeq
7+
Expr.ofSeq(List('{1}, '{2}, '{3}))
88
}

tests/run-macros/tasty-interpolation-1/Macro.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ object Macro {
1515

1616
object SIntepolator extends MacroStringInterpolator[String] {
1717
protected def interpolate(strCtx: StringContext, args: List[Expr[Any]])(given QuoteContext): Expr[String] =
18-
'{(${strCtx}).s(${args.toExprOfList}: _*)}
18+
'{(${strCtx}).s(${Expr.ofList(args)}: _*)}
1919
}
2020

2121
object RawIntepolator extends MacroStringInterpolator[String] {
2222
protected def interpolate(strCtx: StringContext, args: List[Expr[Any]])(given QuoteContext): Expr[String] =
23-
'{(${strCtx}).raw(${args.toExprOfList}: _*)}
23+
'{(${strCtx}).raw(${Expr.ofList(args)}: _*)}
2424
}
2525

2626
object FooIntepolator extends MacroStringInterpolator[String] {
2727
protected def interpolate(strCtx: StringContext, args: List[Expr[Any]])(given QuoteContext): Expr[String] =
28-
'{(${strCtx}).s(${args.map(_ => '{"foo"}).toExprOfList}: _*)}
28+
'{(${strCtx}).s(${Expr.ofList(args.map(_ => '{"foo"}))}: _*)}
2929
}
3030

3131
// TODO put this class in the stdlib or separate project?

tests/run-macros/tasty-simplified/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ object Macros {
1919
}
2020

2121
val tps = unpackTuple(typeOf[T])
22-
tps.map(_.show.toExpr).toExprOfSeq
22+
Expr.ofSeq(tps.map(_.show.toExpr))
2323
}
2424
}

tests/run-macros/tasty-string-interpolation-reporter-test/Macros_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ object Macro {
4444
}
4545
}
4646
fooCore(parts, args, reporter) // Discard result
47-
errors.result().toExprOfList
47+
Expr.ofList(errors.result())
4848
}
4949

5050

@@ -58,7 +58,7 @@ object Macro {
5858
reporter.errorOnPart("Cannot use #", idx)
5959
}
6060

61-
'{ StringContext(${parts.toList.toExprOfList}: _*).s(${args.toList.toExprOfList}: _*) }
61+
'{ StringContext(${Expr.ofList(parts)}: _*).s(${Expr.ofList(args)}: _*) }
6262
}
6363

6464

tests/run-macros/xml-interpolation-2/XmlQuote_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ object XmlQuote {
5959
// [a0, ...]: Any*
6060
val args2: Expr[List[Any]] = args.unseal.underlyingArgument match {
6161
case Typed(Repeated(args0, _), _) => // statically known args, make list directly
62-
args0.map(_.seal).toExprOfList
62+
Expr.ofList(args0.map(_.seal))
6363
case _ =>
6464
'{$args.toList}
6565

0 commit comments

Comments
 (0)