Skip to content

Commit 20a176b

Browse files
committed
Add documentation and test for indirect summons
1 parent 6ad7c71 commit 20a176b

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

Diff for: library/src/scala/quoted/Expr.scala

+11
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,17 @@ object Expr {
280280
}
281281
}
282282

283+
/** Find a given instance of type `T` in the current scope,
284+
* while excluding certain symbols from the initial implicit search.
285+
* Return `Some` containing the expression of the implicit or
286+
* `None` if implicit resolution failed.
287+
*
288+
* @tparam T type of the implicit parameter
289+
* @param ignored Symbols ignored during the initial implicit search
290+
*
291+
* @note if the found given requires additional search for other given instances,
292+
* this additional search will NOT exclude the symbols from the `ignored` list.
293+
*/
283294
@scala.annotation.experimental def summonIgnoring[T](using Type[T])(using quotes: Quotes)(ignored: quotes.reflect.Symbol*): Option[Expr[T]] = {
284295
import quotes.reflect._
285296
Implicits.searchIgnoring(TypeRepr.of[T])(ignored*) match {

Diff for: library/src/scala/quoted/Quotes.scala

+10
Original file line numberDiff line numberDiff line change
@@ -3706,6 +3706,16 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
37063706
*/
37073707
def search(tpe: TypeRepr): ImplicitSearchResult
37083708

3709+
/** Find a given instance of type `T` in the current scope provided by the current enclosing splice,
3710+
* while excluding certain symbols from the initial implicit search.
3711+
* Return an `ImplicitSearchResult`.
3712+
*
3713+
* @param tpe type of the implicit parameter
3714+
* @param ignored Symbols ignored during the initial implicit search
3715+
*
3716+
* @note if an found given requires additional search for other given instances,
3717+
* this additional search will NOT exclude the symbols from the `ignored` list.
3718+
*/
37093719
@experimental def searchIgnoring(tpe: TypeRepr)(ignored: Symbol*): ImplicitSearchResult
37103720
}
37113721

Diff for: tests/run-macros/summonIgnoring-nonrecursive.check

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
TC[C2] generated in macro using:
2+
TC2[_] generated in macro using:
3+
TC[C1] generated in macro
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//> using options -experimental
2+
import scala.quoted._
3+
class C1
4+
trait TC[T] {
5+
def print(): Unit
6+
}
7+
8+
object TC {
9+
implicit transparent inline def auto[T]: TC[T] = ${autoImpl[T]}
10+
def autoImpl[T: Type](using Quotes): Expr[TC[T]] =
11+
import quotes.reflect._
12+
if (TypeRepr.of[T].typeSymbol == Symbol.classSymbol("C1")){
13+
'{
14+
new TC[T] {
15+
def print() = {
16+
println("TC[C1] generated in macro")
17+
}
18+
}
19+
}
20+
} else {
21+
Expr.summonIgnoring[TC2[C1]](Symbol.classSymbol("TC").companionModule.methodMember("auto")*) match
22+
case Some(a) =>
23+
'{
24+
new TC[T] {
25+
def print(): Unit =
26+
println(s"TC[${${Expr(TypeRepr.of[T].show)}}] generated in macro using:")
27+
$a.print()
28+
}
29+
}
30+
case None =>
31+
'{
32+
new TC[T]{
33+
def print(): Unit =
34+
println(s"TC[${${Expr(TypeRepr.of[T].show)}}] generated in macro without TC2[_]")
35+
}
36+
}
37+
}
38+
}
39+
40+
trait TC2[T] {
41+
def print(): Unit
42+
}
43+
44+
object TC2 {
45+
implicit def auto2[T](using tc: TC[T]): TC2[T] = new TC2[T] {
46+
def print(): Unit =
47+
println(s"TC2[_] generated in macro using:")
48+
tc.print()
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//> using options -experimental
2+
3+
@main def Test(): Unit = {
4+
class C2
5+
summon[TC[C2]].print()
6+
}

0 commit comments

Comments
 (0)