File tree 5 files changed +80
-0
lines changed
summonIgnoring-nonrecursive
5 files changed +80
-0
lines changed Original file line number Diff line number Diff line change @@ -280,6 +280,17 @@ object Expr {
280
280
}
281
281
}
282
282
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
+ */
283
294
@ scala.annotation.experimental def summonIgnoring [T ](using Type [T ])(using quotes : Quotes )(ignored : quotes.reflect.Symbol * ): Option [Expr [T ]] = {
284
295
import quotes .reflect ._
285
296
Implicits .searchIgnoring(TypeRepr .of[T ])(ignored* ) match {
Original file line number Diff line number Diff line change @@ -3706,6 +3706,16 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
3706
3706
*/
3707
3707
def search (tpe : TypeRepr ): ImplicitSearchResult
3708
3708
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
+ */
3709
3719
@ experimental def searchIgnoring (tpe : TypeRepr )(ignored : Symbol * ): ImplicitSearchResult
3710
3720
}
3711
3721
Original file line number Diff line number Diff line change
1
+ TC[C2] generated in macro using:
2
+ TC2[_] generated in macro using:
3
+ TC[C1] generated in macro
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
1
+ //> using options -experimental
2
+
3
+ @ main def Test (): Unit = {
4
+ class C2
5
+ summon[TC [C2 ]].print()
6
+ }
You can’t perform that action at this time.
0 commit comments