Skip to content

Commit 0dfa827

Browse files
committed
Add quoted.Exprs and move Value(s) into Expr(s)
As a dual of lifting (`Expr.apply`), unlifitings shoould be placed in the same namespace (`Expr.unapply`). * Move `Values.unapply` to `Exprs.unapply` * Move `Value.unapply` to `Expr.unapply` * Add `Exprs.apply`
1 parent ff476f7 commit 0dfa827

File tree

7 files changed

+75
-76
lines changed

7 files changed

+75
-76
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ It is possible to deconstruct or extract values out of `Expr` using pattern matc
617617
`scala.quoted` contains objects that can help extracting values from `Expr`.
618618

619619
* `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).
620+
* `scala.quoted.Expr`/`scala.quoted.Exprs`: matches an expression of a value (or list of values) and returns the value (or list of values).
621621
* `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.

library/src/scala/quoted/Expr.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ object Expr {
102102
/** Lift a value into an expression containing the construction of that value */
103103
def apply[T](x: T)(using qctx: QuoteContext, lift: Liftable[T]): Expr[T] = lift.toExpr(x)
104104

105+
/** Matches expressions containing values and extracts the value.
106+
*
107+
* Usage:
108+
* ```
109+
* (x: Expr[B]) match {
110+
* case Expr(value) => ... // value: B
111+
* }
112+
* ```
113+
*/
114+
def unapply[T](expr: Expr[T])(using valueOf: ValueOfExpr[T], qxtc: QuoteContext): Option[T] =
115+
valueOf(expr)
116+
105117
/** Lifts this sequence of expressions into an expression of a sequence
106118
*
107119
* Transforms a sequence of expression
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package scala.quoted
22

3-
/** Value expressions */
4-
object Values {
3+
/** Sequences of expressions */
4+
object Exprs {
5+
6+
/** Lift a sequence of values into a sequense of expressions containing the construction of each value */
7+
def apply[T](xs: Seq[T])(using lift: Liftable[T], qctx: QuoteContext): Seq[Expr[T]] =
8+
xs.map(x => Expr(x))
59

610
/** Matches literal sequence of literal constant value expressions and return a sequence of values.
711
*
812
* Usage:
913
* ```scala
1014
* inline def sum(args: Int*): Int = ${ sumExpr('args) }
1115
* def sumExpr(argsExpr: Expr[Seq[Int]])(using QuoteContext): Expr[Int] = argsExpr match
12-
* case Varargs(Values(args)) =>
16+
* case Varargs(Exprs(args)) =>
1317
* // args: Seq[Int]
1418
* ...
1519
* }
@@ -18,8 +22,9 @@ object Values {
1822
def unapply[T](exprs: Seq[Expr[T]])(using valueOf: ValueOfExpr[T], qctx: QuoteContext): Option[Seq[T]] =
1923
exprs.foldRight(Option(List.empty[T])) { (elem, acc) =>
2024
(elem, acc) match {
21-
case (Value(value), Some(lst)) => Some(value :: lst)
25+
case (Expr(value), Some(lst)) => Some(value :: lst)
2226
case (_, _) => None
2327
}
2428
}
29+
2530
}

library/src/scala/quoted/Value.scala

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

library/src/scala/quoted/ValueOfExpr.scala

Lines changed: 49 additions & 49 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ object ValueSeq {
1616
* }
1717
* ```
1818
*/
19-
@deprecated("use scala.quoted.Varargs(scala.quoted.Value(_)) instead", "0.23.0")
19+
@deprecated("use scala.quoted.Varargs(scala.quoted.Exprs(_)) instead", "0.23.0")
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(Values(elems)) => Some(elems)
23+
case Varargs(Exprs(elems)) => Some(elems)
2424
case _ => None
2525

2626
}

library/src/scala/quoted/matching/package.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ package object matching {
2323
@deprecated("use scala.quoted.Lambda instead", "0.23.0")
2424
val Lambda: quoted.Lambda.type = quoted.Lambda
2525

26-
@deprecated("use scala.quoted.Value instead", "0.23.0")
27-
val Value: quoted.Value.type = quoted.Value
26+
@deprecated("use scala.quoted.Expr instead", "0.23.0")
27+
val Value: quoted.Expr.type = quoted.Expr
2828

2929
@deprecated("use scala.quoted.ValueOfExpr instead", "0.23.0")
3030
val ValueOfExpr: quoted.ValueOfExpr.type = quoted.ValueOfExpr

0 commit comments

Comments
 (0)