File tree 11 files changed +65
-53
lines changed
docs/docs/reference/metaprogramming
src-bootstrapped/dotty/internal
quote-matcher-string-interpolator-3
11 files changed +65
-53
lines changed Original file line number Diff line number Diff line change @@ -616,15 +616,15 @@ It is possible to deconstruct or extract values out of `Expr` using pattern matc
616
616
617
617
` scala.quoted ` contains objects that can help extracting values from ` Expr ` .
618
618
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]] ` .
622
622
623
623
These could be used in the following way to optimize any call to ` sum ` that has statically known values.
624
624
``` scala
625
625
inline def sum (inline args : Int * ): Int = $ { sumExpr(' args ) }
626
626
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]
628
628
Expr (args.sum) // precompute result of sum
629
629
case Varargs (argExprs) => // argExprs is of type Seq[Expr[Int]]
630
630
val staticSum : Int = argExprs.map {
Original file line number Diff line number Diff line change @@ -66,7 +66,7 @@ object StringContextMacro {
66
66
def splitParts (seq : Expr [Seq [String ]]) = seq match {
67
67
case Varargs (p1) =>
68
68
p1 match
69
- case Const (p2) => (p1.toList, p2.toList)
69
+ case Consts (p2) => (p1.toList, p2.toList)
70
70
case _ => notStatic
71
71
case _ => notStatic
72
72
}
Original file line number Diff line number Diff line change @@ -26,24 +26,4 @@ object Const {
26
26
rec(expr.unseal)
27
27
}
28
28
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
-
49
29
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -15,23 +15,4 @@ object Value {
15
15
def unapply [T ](expr : Expr [T ])(using valueOf : ValueOfExpr [T ], qxtc : QuoteContext ): Option [T ] =
16
16
valueOf(expr)
17
17
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
- }
37
18
}
Original file line number Diff line number Diff line change @@ -42,8 +42,8 @@ object ValueOfExpr {
42
42
43
43
given StringContext_delegate as ValueOfExpr [StringContext ] = new {
44
44
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 : _* ))
47
47
case _ => None
48
48
}
49
49
override def toString (): String = " scala.quoted.ValueOfExpr.Tuple1_delegate"
@@ -270,9 +270,9 @@ object ValueOfExpr {
270
270
271
271
given Seq_delegate [T ](using Type [T ], ValueOfExpr [T ]) as ValueOfExpr [Seq [T ]] = new {
272
272
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)
276
276
case _ => None
277
277
}
278
278
override def toString (): String = " scala.quoted.ValueOfExpr.Seq_delegate"
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ object ConstSeq {
20
20
def unapply [T ](expr : Expr [Seq [T ]])(using qctx : QuoteContext ): Option [Seq [T ]] =
21
21
import scala .quoted .Const
22
22
expr match
23
- case Varargs (Const (elems)) => Some (elems)
23
+ case Varargs (Consts (elems)) => Some (elems)
24
24
case _ => None
25
25
26
26
}
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ object ValueSeq {
20
20
def unapply [T ](expr : Expr [Seq [T ]])(using valueOf : ValueOfExpr [T ], qctx : QuoteContext ): Option [Seq [T ]] =
21
21
import scala .quoted .Const
22
22
expr match
23
- case Varargs (Value (elems)) => Some (elems)
23
+ case Varargs (Values (elems)) => Some (elems)
24
24
case _ => None
25
25
26
26
}
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ object Macros {
8
8
9
9
private def impl (self : Expr [StringContext ], args : Expr [Seq [String ]])(using QuoteContext ): Expr [String ] = {
10
10
self match {
11
- case ' { StringContext ($ {Varargs (Const (parts))}: _* ) } =>
11
+ case ' { StringContext ($ {Varargs (Consts (parts))}: _* ) } =>
12
12
val upprerParts : List [String ] = parts.toList.map(_.toUpperCase)
13
13
val upprerPartsExpr : Expr [List [String ]] = Expr .ofList(upprerParts.map(Expr (_)))
14
14
' { StringContext ($upprerPartsExpr : _* ).s($args : _* ) }
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ private def sumExprShow(argsExpr: Expr[Seq[Int]]) (using QuoteContext): Expr[Str
12
12
private def sumExpr (argsExpr : Expr [Seq [Int ]])(using qctx : QuoteContext ) : Expr [Int ] = {
13
13
import qctx .tasty .{given _ , _ }
14
14
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]
16
16
Expr (args.sum) // precompute result of sum
17
17
case Varargs (argExprs) => // argExprs is of type Seq[Expr[Int]]
18
18
val staticSum : Int = argExprs.map {
You can’t perform that action at this time.
0 commit comments