Skip to content

Commit be3f2d7

Browse files
committed
Define Consts and Values
1 parent 869b9bd commit be3f2d7

File tree

11 files changed

+65
-53
lines changed

11 files changed

+65
-53
lines changed

docs/docs/reference/metaprogramming/macros.md

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

617617
`scala.quoted` contains objects that can help extracting values from `Expr`.
618618

619-
* `scala.quoted.Const`: matches an expression of a literal value (or list of values) and returns the value (or list of values).
620-
* `scala.quoted.Value`: matches an expression of a value (or list of values) and returns the value (or list of values).
621-
* `scala.quoted.Exprs`: 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]]`.
619+
* `scala.quoted.Const`/`scala.quoted.Consts`: matches an expression of a literal value (or list of values) and returns the value (or list of values).
620+
* `scala.quoted.Value`/`scala.quoted.Values`: matches an expression of a value (or list of values) and returns the value (or list of values).
621+
* `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]]`.
622622

623623
These could be used in the following way to optimize any call to `sum` that has statically known values.
624624
```scala
625625
inline def sum(inline args: Int*): Int = ${ sumExpr('args) }
626626
private def sumExpr(argsExpr: Expr[Seq[Int]])(using QuoteContext): Expr[Int] = argsExpr match {
627-
case Varargs(Const(args)) => // args is of type Seq[Int]
627+
case Varargs(Consts(args)) => // args is of type Seq[Int]
628628
Expr(args.sum) // precompute result of sum
629629
case Varargs(argExprs) => // argExprs is of type Seq[Expr[Int]]
630630
val staticSum: Int = argExprs.map {

library/src-bootstrapped/dotty/internal/StringContextMacro.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ object StringContextMacro {
6666
def splitParts(seq: Expr[Seq[String]]) = seq match {
6767
case Varargs(p1) =>
6868
p1 match
69-
case Const(p2) => (p1.toList, p2.toList)
69+
case Consts(p2) => (p1.toList, p2.toList)
7070
case _ => notStatic
7171
case _ => notStatic
7272
}

library/src/scala/quoted/Const.scala

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,4 @@ object Const {
2626
rec(expr.unseal)
2727
}
2828

29-
/** Matches literal sequence of literal constant value expressions and return a sequence of values.
30-
*
31-
* Usage:
32-
* ```scala
33-
* inline def sum(args: Int*): Int = ${ sumExpr('args) }
34-
* def sumExpr(argsExpr: Expr[Seq[Int]])(usingusing QuoteContext): Expr[Int] = argsExpr match
35-
* case Varargs(Const(args)) =>
36-
* // args: Seq[Int]
37-
* ...
38-
* }
39-
* ```
40-
*/
41-
def unapply[T](exprs: Seq[Expr[T]])(using qctx: QuoteContext): Option[Seq[T]] =
42-
exprs.foldRight(Option(List.empty[T])) { (elem, acc) =>
43-
(elem, acc) match {
44-
case (Const(value), Some(lst)) => Some(value :: lst)
45-
case (_, _) => None
46-
}
47-
}
48-
4929
}

library/src/scala/quoted/Consts.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package scala.quoted
2+
3+
/** MLiteral constant values */
4+
object Consts {
5+
6+
/** Matches literal sequence of literal constant value expressions and return a sequence of values.
7+
*
8+
* Usage:
9+
* ```scala
10+
* inline def sum(args: Int*): Int = ${ sumExpr('args) }
11+
* def sumExpr(argsExpr: Expr[Seq[Int]])(usingusing QuoteContext): Expr[Int] = argsExpr match
12+
* case Varargs(Consts(args)) =>
13+
* // args: Seq[Int]
14+
* ...
15+
* }
16+
* ```
17+
*/
18+
def unapply[T](exprs: Seq[Expr[T]])(using qctx: QuoteContext): Option[Seq[T]] =
19+
exprs.foldRight(Option(List.empty[T])) { (elem, acc) =>
20+
(elem, acc) match {
21+
case (Const(value), Some(lst)) => Some(value :: lst)
22+
case (_, _) => None
23+
}
24+
}
25+
26+
}

library/src/scala/quoted/Value.scala

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,4 @@ object Value {
1515
def unapply[T](expr: Expr[T])(using valueOf: ValueOfExpr[T], qxtc: QuoteContext): Option[T] =
1616
valueOf(expr)
1717

18-
/** Matches literal sequence of literal constant value expressions and return a sequence of values.
19-
*
20-
* Usage:
21-
* ```scala
22-
* inline def sum(args: Int*): Int = ${ sumExpr('args) }
23-
* def sumExpr(argsExpr: Expr[Seq[Int]])(using QuoteContext): Expr[Int] = argsExpr match
24-
* case Varargs(Value(args)) =>
25-
* // args: Seq[Int]
26-
* ...
27-
* }
28-
* ```
29-
*/
30-
def unapply[T](exprs: Seq[Expr[T]])(using valueOf: ValueOfExpr[T], qctx: QuoteContext): Option[Seq[T]] =
31-
exprs.foldRight(Option(List.empty[T])) { (elem, acc) =>
32-
(elem, acc) match {
33-
case (Value(value), Some(lst)) => Some(value :: lst)
34-
case (_, _) => None
35-
}
36-
}
3718
}

library/src/scala/quoted/ValueOfExpr.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ object ValueOfExpr {
4242

4343
given StringContext_delegate as ValueOfExpr[StringContext] = new {
4444
def apply(x: Expr[StringContext])(using qctx: QuoteContext): Option[StringContext] = x match {
45-
case '{ new StringContext(${Varargs(Const(args))}: _*) } => Some(StringContext(args: _*))
46-
case '{ StringContext(${Varargs(Const(args))}: _*) } => Some(StringContext(args: _*))
45+
case '{ new StringContext(${Varargs(Consts(args))}: _*) } => Some(StringContext(args: _*))
46+
case '{ StringContext(${Varargs(Consts(args))}: _*) } => Some(StringContext(args: _*))
4747
case _ => None
4848
}
4949
override def toString(): String = "scala.quoted.ValueOfExpr.Tuple1_delegate"
@@ -270,9 +270,9 @@ object ValueOfExpr {
270270

271271
given Seq_delegate[T](using Type[T], ValueOfExpr[T]) as ValueOfExpr[Seq[T]] = new {
272272
def apply(x: Expr[Seq[T]])(using qctx: QuoteContext): Option[Seq[T]] = x match {
273-
case Varargs(Value(elems)) => Some(elems)
274-
case '{ scala.collection.Seq[T](${Varargs(Value(elems))}: _*) } => Some(elems)
275-
case '{ scala.collection.immutable.Seq[T](${Varargs(Value(elems))}: _*) } => Some(elems)
273+
case Varargs(Values(elems)) => Some(elems)
274+
case '{ scala.collection.Seq[T](${Varargs(Values(elems))}: _*) } => Some(elems)
275+
case '{ scala.collection.immutable.Seq[T](${Varargs(Values(elems))}: _*) } => Some(elems)
276276
case _ => None
277277
}
278278
override def toString(): String = "scala.quoted.ValueOfExpr.Seq_delegate"

library/src/scala/quoted/Values.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package scala.quoted
2+
3+
/** Value expressions */
4+
object Values {
5+
6+
/** Matches literal sequence of literal constant value expressions and return a sequence of values.
7+
*
8+
* Usage:
9+
* ```scala
10+
* inline def sum(args: Int*): Int = ${ sumExpr('args) }
11+
* def sumExpr(argsExpr: Expr[Seq[Int]])(using QuoteContext): Expr[Int] = argsExpr match
12+
* case Varargs(Values(args)) =>
13+
* // args: Seq[Int]
14+
* ...
15+
* }
16+
* ```
17+
*/
18+
def unapply[T](exprs: Seq[Expr[T]])(using valueOf: ValueOfExpr[T], qctx: QuoteContext): Option[Seq[T]] =
19+
exprs.foldRight(Option(List.empty[T])) { (elem, acc) =>
20+
(elem, acc) match {
21+
case (Value(value), Some(lst)) => Some(value :: lst)
22+
case (_, _) => None
23+
}
24+
}
25+
}

library/src/scala/quoted/matching/ConstSeq.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object ConstSeq {
2020
def unapply[T](expr: Expr[Seq[T]])(using qctx: QuoteContext): Option[Seq[T]] =
2121
import scala.quoted.Const
2222
expr match
23-
case Varargs(Const(elems)) => Some(elems)
23+
case Varargs(Consts(elems)) => Some(elems)
2424
case _ => None
2525

2626
}

library/src/scala/quoted/matching/ValueSeq.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object ValueSeq {
2020
def unapply[T](expr: Expr[Seq[T]])(using valueOf: ValueOfExpr[T], qctx: QuoteContext): Option[Seq[T]] =
2121
import scala.quoted.Const
2222
expr match
23-
case Varargs(Value(elems)) => Some(elems)
23+
case Varargs(Values(elems)) => Some(elems)
2424
case _ => None
2525

2626
}

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
@@ -8,7 +8,7 @@ object Macros {
88

99
private def impl(self: Expr[StringContext], args: Expr[Seq[String]])(using QuoteContext): Expr[String] = {
1010
self match {
11-
case '{ StringContext(${Varargs(Const(parts))}: _*) } =>
11+
case '{ StringContext(${Varargs(Consts(parts))}: _*) } =>
1212
val upprerParts: List[String] = parts.toList.map(_.toUpperCase)
1313
val upprerPartsExpr: Expr[List[String]] = Expr.ofList(upprerParts.map(Expr(_)))
1414
'{ StringContext($upprerPartsExpr: _*).s($args: _*) }

tests/run-macros/quoted-matching-docs/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private def sumExprShow(argsExpr: Expr[Seq[Int]]) (using QuoteContext): Expr[Str
1212
private def sumExpr(argsExpr: Expr[Seq[Int]])(using qctx: QuoteContext) : Expr[Int] = {
1313
import qctx.tasty.{given _, _}
1414
UnsafeExpr.underlyingArgument(argsExpr) match {
15-
case Varargs(Const(args)) => // args is of type Seq[Int]
15+
case Varargs(Consts(args)) => // args is of type Seq[Int]
1616
Expr(args.sum) // precompute result of sum
1717
case Varargs(argExprs) => // argExprs is of type Seq[Expr[Int]]
1818
val staticSum: Int = argExprs.map {

0 commit comments

Comments
 (0)