-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
1 parent
d0403b6
commit ddf41a0
Showing
5 changed files
with
74 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Macros.scala | ||
import Main._ | ||
import scala.quoted.* | ||
|
||
object Macros { | ||
inline def apply(): ProviderProcessor = | ||
${ Macros.processorExpr } | ||
|
||
def processorExpr[I: Type](using q: Quotes): Expr[ProviderProcessor] = '{ | ||
new ProviderProcessor { | ||
override def apply(simple: Simple): MyF[Int] = | ||
${ Macros.methodProcessorImpl('simple) } | ||
} | ||
} | ||
|
||
def methodProcessorImpl[I: Type](using q: Quotes)(service: Expr[Simple]): Expr[MyF[Int]] = { | ||
import q.reflect._ | ||
|
||
val returnType = TypeRepr.of[Int] | ||
returnType.asType match { | ||
case '[rt] => | ||
'{ | ||
${ | ||
import quotes.reflect._ | ||
TypeApply( | ||
Select.unique('{ ???.asInstanceOf[Codec] }.asTerm, "apply"), | ||
List(TypeTree.of[rt]) // generates the error, directly using Int instead of rt makes it disappear | ||
).asExpr | ||
} | ||
??? | ||
} | ||
} | ||
} | ||
} |
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,20 @@ | ||
// Main.scala | ||
object Main { | ||
type MyF[A] | ||
|
||
trait ProviderProcessor { | ||
def apply(simple: Simple): MyF[Int] | ||
} | ||
|
||
trait Codec { | ||
def apply[A]: MyF[A] | ||
} | ||
|
||
trait Simple { | ||
def a0: Int | ||
} | ||
|
||
def test(): Unit = { | ||
val p= Macros() | ||
} | ||
} |
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 @@ | ||
import scala.quoted.* | ||
|
||
object Macro: | ||
transparent inline def foo: Any = ${ fooImpl } | ||
|
||
def fooImpl(using Quotes): Expr[Any] = | ||
import quotes.reflect.* | ||
'{ | ||
val xxx = ${ | ||
Type.of[Int] match | ||
case '[tpe] => | ||
Typed(Expr(1).asTerm, TypeTree.of[tpe]).asExpr | ||
} | ||
xxx | ||
} |
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,2 @@ | ||
@main def run() = | ||
println(Macro.foo) |