File tree Expand file tree Collapse file tree 11 files changed +62
-50
lines changed
docs/docs/reference/metaprogramming
src-bootstrapped/dotty/internal
quote-matcher-string-interpolator-3 Expand file tree Collapse file tree 11 files changed +62
-50
lines changed Original file line number Diff line number Diff line change @@ -624,7 +624,7 @@ These could be used in the following way to optimize any call to `sum` that has
624624``` scala
625625inline def sum (inline args : Int * ): Int = $ { sumExpr(' args ) }
626626private 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 {
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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}
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 {
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}
Original file line number Diff line number Diff 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"
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 {
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}
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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 : _* ) }
You can’t perform that action at this time.
0 commit comments