我正在尝试访问作为宏实现的方法的参数。
object Macros {
def impl()(using Quotes): Expr[Unit] = {
import quotes.reflect._
val params: List[List[ValDef]] = {
def nearestEnclosingMethodParams(owner: Symbol): List[List[ValDef]] =
owner match {
case defSym if defSym.isDefDef =>
defSym.tree.asInstanceOf[DefDef].paramss
case _ =>
nearestEnclosingMethod(owner.owner)
}
nearestEnclosingMethodParams(Symbol.spliceOwner)
}
println(params) // I would do something useful with params names and types here
'{()}
}
}
呼叫站点可能类似于:
object Test {
def foo(a: String, b: Int) = Foo.impl
@main def run(): Unit = {
val x = foo("blah", 24)
()
}
}
object Foo {
inline def impl = ${ Macros.impl() }
}
现在,CyclicReference
当宏展开时,我在defSym.tree
. 我知道这defSym.tree
是循环的,因为它包含当前扩展宏的代码,但我仍然需要访问方法定义的“树”版本来访问其名称和参数,而不需要方法的主体。我怎样才能在不骑自行车的情况下获得这些信息?