Skip to content

Commit db709ac

Browse files
Merge pull request #6651 from dotty-staging/add-constSeq-extractor
Add quote ConstSeq extractor
2 parents 74ec0ca + 47ff69a commit db709ac

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package scala.quoted.matching
2+
3+
import scala.quoted.Expr
4+
5+
import scala.tasty.Reflection // TODO do not depend on reflection directly
6+
7+
/** Literal sequence of literal constant value expressions */
8+
object ConstSeq {
9+
10+
/** Matches literal sequence of literal constant value expressions */
11+
def unapply[T](expr: Expr[Seq[T]]) given Reflection: Option[Seq[T]] = expr match {
12+
case ExprSeq(elems) =>
13+
elems.foldRight(Option(List.empty[T])) { (elem, acc) =>
14+
(elem, acc) match {
15+
case (Const(value), Some(lst)) => Some(value :: lst)
16+
case (_, _) => None
17+
}
18+
}
19+
case _ => None
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
HELLO WORLD
2+
HELLO World
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import scala.quoted._
2+
import scala.quoted.matching._
3+
4+
import scala.tasty.Reflection
5+
6+
object Macros {
7+
8+
inline def (self: => StringContext) S(args: => String*): String = ${impl('self, 'args)}
9+
10+
private def impl(self: Expr[StringContext], args: Expr[Seq[String]]) given Reflection: Expr[String] = {
11+
self match {
12+
case '{ StringContext(${ConstSeq(parts)}: _*) } =>
13+
val upprerParts: List[String] = parts.toList.map(_.toUpperCase)
14+
val upprerPartsExpr: Expr[List[String]] = upprerParts.map(_.toExpr).toExprOfList
15+
'{ StringContext($upprerPartsExpr: _*).s($args: _*) }
16+
case _ =>
17+
'{
18+
val parts: Seq[String] = $self.parts
19+
val upprerParts = parts.map(_.toUpperCase)
20+
StringContext(upprerParts: _*).s($args: _*)
21+
}
22+
}
23+
24+
}
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Macros._
2+
3+
object Test {
4+
5+
def main(args: Array[String]): Unit = {
6+
println(S"Hello World")
7+
println(S"Hello ${"World"}")
8+
}
9+
10+
}

0 commit comments

Comments
 (0)