Skip to content

Commit 1e397e0

Browse files
committed
Rename quoted.matching.searchImplicitExpr to summonExpr
1 parent 85dd873 commit 1e397e0

File tree

9 files changed

+79
-7
lines changed

9 files changed

+79
-7
lines changed

docs/docs/reference/metaprogramming/macros.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -569,13 +569,13 @@ sum
569569
### Find implicits within a macro
570570

571571
Similarly to the `summonFrom` construct, it is possible to make implicit search available
572-
in a quote context. For this we simply provide `scala.quoted.matching.searchImplicitExpr:
572+
in a quote context. For this we simply provide `scala.quoted.matching.summonExpr:
573573

574574
```scala
575575
inline def setFor[T]: Set[T] = ${ setForExpr[T] }
576576

577577
def setForExpr[T: Type](given QuoteContext): Expr[Set[T]] = {
578-
searchImplicitExpr[Ordering[T]] match {
578+
summonExpr[Ordering[T]] match {
579579
case Some(ord) => '{ new TreeSet[T]()($ord) }
580580
case _ => '{ new HashSet[T] }
581581
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package object matching {
1010
* @param tpe quoted type of the implicit parameter
1111
* @param qctx current context
1212
*/
13-
def searchImplicitExpr[T](given tpe: Type[T], qctx: QuoteContext): Option[Expr[T]] = {
13+
def summonExpr[T](given tpe: Type[T], qctx: QuoteContext): Option[Expr[T]] = {
1414
import qctx.tasty.{_, given}
1515
searchImplicit(tpe.unseal.tpe) match {
1616
case iss: ImplicitSearchSuccess => Some(iss.tree.seal.asInstanceOf[Expr[T]])

library/src/scala/tasty/Reflection.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ class Reflection(private[scala] val internal: CompilerInterface)
2525
with TypeOrBoundsOps { self =>
2626

2727
def typeOf[T: scala.quoted.Type]: Type =
28-
implicitly[scala.quoted.Type[T]].unseal.tpe
28+
summon[scala.quoted.Type[T]].unseal.tpe
2929

3030
}

tests/run-macros/quote-implicitMatch/Macro_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.quoted.matching._
66
inline def f1[T]() = ${ f1Impl[T] }
77

88
def f1Impl[T: Type](given QuoteContext) = {
9-
searchImplicitExpr[Ordering[T]] match {
9+
summonExpr[Ordering[T]] match {
1010
case Some(ord) => '{ new TreeSet[T]()($ord) }
1111
case _ => '{ new HashSet[T] }
1212
}
@@ -18,7 +18,7 @@ class B
1818
inline def g = ${ gImpl }
1919

2020
def gImpl(given QuoteContext) = {
21-
if (searchImplicitExpr[A].isDefined) '{ println("A") }
22-
else if (searchImplicitExpr[B].isDefined) '{ println("B") }
21+
if (summonExpr[A].isDefined) '{ println("A") }
22+
else if (summonExpr[B].isDefined) '{ println("B") }
2323
else throw new MatchError("")
2424
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Int(1) Str(abc)
2+
Int(1) Str(abc)
3+
xyz
4+
Int(1) Str(xyz)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import scala.quoted._
2+
import scala.quoted.matching._
3+
4+
inline def (sc: StringContext) showMe(args: =>Any*): String = ${ showMeExpr('sc, 'args) }
5+
6+
private def showMeExpr(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]])(given qctx: QuoteContext): Expr[String] = {
7+
argsExpr match {
8+
case ExprSeq(argExprs) =>
9+
val argShowedExprs = argExprs.map {
10+
case '{ $arg: $tp } =>
11+
val showTp = '[Show[$tp]]
12+
summonExpr(given showTp, summon[QuoteContext]) match {
13+
case Some(showExpr) => '{ $showExpr.show($arg) }
14+
case None => qctx.error(s"could not find implicit for ${showTp.show}", arg); '{???}
15+
}
16+
}
17+
val newArgsExpr = Expr.ofSeq(argShowedExprs)
18+
'{ $sc.s($newArgsExpr: _*) }
19+
case _ =>
20+
// `new StringContext(...).showMeExpr(args: _*)` not an explicit `showMeExpr"..."`
21+
qctx.error(s"Args must be explicit", argsExpr)
22+
'{???}
23+
}
24+
}
25+
26+
trait Show[-T] {
27+
def show(x: T): String
28+
}
29+
30+
given Show[Int] = x => s"Int($x)"
31+
given Show[String] = x => s"Str($x)"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object Test {
2+
3+
def main(args: Array[String]): Unit = {
4+
println(showMe"${1: Int} ${"abc": String}")
5+
println(showMe"${1} ${"abc"}")
6+
println(showMe"${1} ${println("xyz"); "xyz"}")
7+
}
8+
9+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Int(1) Str(abc)
2+
Int(1) Str(abc)
3+
xyz
4+
Int(1) Str(xyz)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
object Lib {
3+
def (sc: StringContext) showMe(args: Showed*): String = sc.s(args: _*)
4+
5+
opaque type Showed = String
6+
7+
given [T](given show: Show[T]): Conversion[T, Showed] = x => show(x)
8+
9+
trait Show[T] {
10+
def apply(x: T): String
11+
}
12+
13+
given Show[Int] = x => s"Int($x)"
14+
given Show[String] = x => s"Str($x)"
15+
}
16+
object Test {
17+
import Lib._
18+
def main(args: Array[String]): Unit = {
19+
println(showMe"${1: Int} ${"abc": String}")
20+
println(showMe"${1} ${"abc"}")
21+
println(showMe"${1} ${println("xyz"); "xyz"}")
22+
}
23+
24+
}

0 commit comments

Comments
 (0)