以下宏是从一个更大的示例中提取的,它应该创建一个只有对 的引用的树this
:
def echoThisImpl(c:Context): c.Expr[Any] = {
import c.universe._
val selfTree = This(c.enclosingClass.symbol)
c.Expr[AnyRef](selfTree)
}
def echoThis: Any = macro CallMacro.echoThisImpl
但是调用echoThis
诸如
object Testing extends App {
val thisValue = CallMacro.echoThis
println(thisValue)
}
编译失败,提示信息
[error] /home/rafael/dev/scala/goose/goose-macros/src/test/scala/Testing.scala:8: type mismatch;
[error] found : <noprefix>
[error] required: Any
[error] val thisValue = CallMacro.echoThis
如果我设置-Ymacro-debug-lite标志,生成的树是This(newTermName("<local Testing>"))
.