Skip to content

Commit e2d1090

Browse files
committed
Remove quoted.Const.unapply
This extractor was replaced with the more versatile extractor `quoted.Expr.unapply`. Usually, it is better to use `expr.value`/`expr.valueOrError` when it is simpler to do so. * Remove definition of `quoted.Const.unapply` * Replace uses of `Const` with `Expr` **Migration** * Consider using `expr.value` and them match on `Option` * Consider using `expr.valueOrError` * Replace `Const(x)` with `Expr(x)` if the type is known * Replace `Const(x: Int)` with `'{ ${Expr(x)}: Int }` if type of expression must be checked at runtime
1 parent 121ced4 commit e2d1090

File tree

23 files changed

+35
-99
lines changed

23 files changed

+35
-99
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,15 +634,15 @@ It is possible to deconstruct or extract values out of `Expr` using pattern matc
634634

635635
* `scala.quoted.Expr`/`scala.quoted.Exprs`: matches an expression of a value (or list of values) and returns the value (or list of values).
636636
* `scala.quoted.Const`/`scala.quoted.Consts`: Same as `Expr`/`Exprs` but only works on primitive values.
637-
* `scala.quoted.Varargs`: matches an explicit sequence of expressions and returns them. These sequences are useful to get individual `Expr[T]` out of a varargs expression of type `Expr[Seq[T]]`.
637+
* `scala.quoted.Varargs`: matches an explicit sequence of expresions and returns them. These sequences are useful to get individual `Expr[T]` out of a varargs expression of type `Expr[Seq[T]]`.
638638

639639

640640
These could be used in the following way to optimize any call to `sum` that has statically known values.
641641
```scala
642642
inline def sum(inline args: Int*): Int = ${ sumExpr('args) }
643643
private def sumExpr(argsExpr: Expr[Seq[Int]])(using Quotes): Expr[Int] = argsExpr match {
644-
case Varargs(args @ Exprs(argValues)) =>
645-
// args is of type Seq[Expr[Int]]
644+
case Varargs(argExprs @ Exprs(argValues)) =>
645+
// argExprs is of type Seq[Expr[Int]]
646646
// argValues is of type Seq[Int]
647647
Expr(argValues.sum) // precompute result of sum
648648
case Varargs(argExprs) => // argExprs is of type Seq[Expr[Int]]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ object Expr {
3232
Block(statements.map(Term.of), Term.of(expr)).asExpr.asInstanceOf[Expr[T]]
3333
}
3434

35-
/** Creates an expression that will construct the value `x` */
35+
/** Lift a value into an expression containing the construction of that value */
3636
def apply[T](x: T)(using ToExpr[T])(using Quotes): Expr[T] =
3737
scala.Predef.summon[ToExpr[T]].apply(x)
3838

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ object FromExpr {
136136
*/
137137
given StringContextFromExpr: FromExpr[StringContext] with {
138138
def unapply(x: Expr[StringContext])(using Quotes) = x match {
139-
case '{ new StringContext(${Varargs(Consts(args))}: _*) } => Some(StringContext(args: _*))
140-
case '{ StringContext(${Varargs(Consts(args))}: _*) } => Some(StringContext(args: _*))
139+
case '{ new StringContext(${Varargs(Exprs(args))}: _*) } => Some(StringContext(args: _*))
140+
case '{ StringContext(${Varargs(Exprs(args))}: _*) } => Some(StringContext(args: _*))
141141
case _ => None
142142
}
143143
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
package scala.quoted
22

33
abstract class Expr[+T] private[scala]
4+
5+
object Expr:
6+
def unapply[T](x: Expr[T])(using FromExpr[T])(using Quotes): Option[T] = ???

library/src/scala/quoted/Const.scala

Lines changed: 0 additions & 38 deletions
This file was deleted.

library/src/scala/quoted/Consts.scala

Lines changed: 0 additions & 28 deletions
This file was deleted.

library/src/scala/quoted/Exprs.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package scala.quoted
22

3+
/** Value expressions */
34
object Exprs:
45

56
/** Matches literal sequence of literal constant value expressions and return a sequence of values.
@@ -9,7 +10,6 @@ object Exprs:
910
* inline def sum(args: Int*): Int = ${ sumExpr('args) }
1011
* def sumExpr(argsExpr: Expr[Seq[Int]])(using Quotes): Expr[Int] = argsExpr match
1112
* case Varargs(Exprs(args)) =>
12-
* case Varargs(Exprs(args)) =>
1313
* // args: Seq[Int]
1414
* ...
1515
* }

tests/bench/string-interpolation-macro/Macro.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macro {
99
var res: Expr[String] = null
1010
for _ <- 0 to 5_000 do
1111
(strCtxExpr, argsExpr) match {
12-
case ('{ StringContext(${Varargs(Consts(parts))}: _*) }, Varargs(Consts(args))) =>
12+
case ('{ StringContext(${Varargs(Exprs(parts))}: _*) }, Varargs(Exprs(args))) =>
1313
res = Expr(StringContext(parts: _*).s(args: _*))
1414
case _ => ???
1515
}

tests/neg-macros/i6432/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macro {
99
import quotes.reflect._
1010
sc match {
1111
case '{ StringContext(${Varargs(parts)}: _*) } =>
12-
for (part @ Const(s) <- parts)
12+
for (part @ Expr(s) <- parts)
1313
report.error(s, Term.of(part).pos)
1414
}
1515
'{}

tests/neg-macros/i6432b/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Macro {
99
import quotes.reflect._
1010
sc match {
1111
case '{ StringContext(${Varargs(parts)}: _*) } =>
12-
for (part @ Const(s) <- parts)
12+
for (part @ Expr(s) <- parts)
1313
report.error(s, Term.of(part).pos)
1414
}
1515
'{}

0 commit comments

Comments
 (0)