forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
100 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
No given in scope: | ||
TC[C2] generated in macro without TC[C1] | ||
Given in scope: | ||
TC[C2] generated in macro using: | ||
TC[C1] defined by a user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//> using options -experimental | ||
import scala.quoted._ | ||
class C1 | ||
trait TC[T] { | ||
def print(): Unit | ||
} | ||
object TC { | ||
implicit transparent inline def auto[T]: TC[T] = ${autoImpl[T]} | ||
def autoImpl[T: Type](using Quotes): Expr[TC[T]] = | ||
import quotes.reflect._ | ||
if(TypeRepr.of[T].typeSymbol == Symbol.classSymbol("C1")){ | ||
'{ | ||
new TC[T] { | ||
def print() = { | ||
println("TC[C1] generated in macro") | ||
} | ||
} | ||
} | ||
} else { | ||
Expr.summonIgnoring[TC[C1]](Symbol.classSymbol("TC").companionModule.methodMember("auto")*) match | ||
case Some(a) => | ||
'{ | ||
new TC[T] { | ||
def print(): Unit = | ||
println(s"TC[${${Expr(TypeRepr.of[T].show)}}] generated in macro using:") | ||
$a.print() | ||
} | ||
} | ||
case None => | ||
'{ | ||
new TC[T]{ | ||
def print(): Unit = | ||
println(s"TC[${${Expr(TypeRepr.of[T].show)}}] generated in macro without TC[C1]") | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//> using options -experimental | ||
|
||
@main def Test(): Unit = { | ||
class C2 | ||
println("No given in scope:") | ||
summon[TC[C2]].print() | ||
|
||
{ | ||
println("Given in scope:") | ||
given TC[C1] = new TC[C1] { | ||
def print() = println("TC[C1] defined by a user") | ||
} | ||
summon[TC[C2]].print() | ||
} | ||
} |