Eri*_*ric 7 scala scala-macros
这是我上一个问题的后续行动.
我想像下面的代码一样工作.我希望能够生成一个宏生成的方法:
case class Cat()
test[Cat].method(1)
Run Code Online (Sandbox Code Playgroud)
生成方法本身的实现使用宏("吸血鬼"方法):
// macro call
def test[T] = macro testImpl[T]
// macro implementation
def testImpl[T : c.WeakTypeTag](c: Context): c.Expr[Any] = {
import c.universe._
val className = newTypeName("Test")
// IS IT POSSIBLE TO CALL `otherMethod` HERE?
val bodyInstance = q"(p: Int) => otherMethod(p * 2)"
c.Expr { q"""
class $className {
protected val aValue = 1
@body($bodyInstance)
def method(p: Int): Int = macro methodImpl[Int]
def otherMethod(p: Int): Int = p
}
new $className {}
"""}
}
// method implementation
def methodImpl[F](c: Context)(p: c.Expr[F]): c.Expr[F] = {
import c.universe._
val field = c.macroApplication.symbol
val bodyAnnotation = field.annotations.filter(_.tpe <:< typeOf[body]).head
c.Expr(q"${bodyAnnotation.scalaArgs.head}.apply(${p.tree.duplicate})")
}
Run Code Online (Sandbox Code Playgroud)
此代码无法编译:
[error] no-symbol does not have an owner
last tree to typer: This(anonymous class $anonfun)
[error] symbol: anonymous class $anonfun (flags: final <synthetic>)
[error] symbol definition: final class $anonfun extends AbstractFunction1$mcII$sp with Serializable
[error] tpe: examples.MacroMatcherSpec.Test.$anonfun.type
[error] symbol owners: anonymous class $anonfun -> value <local Test> -> class Test -> method e1 -> class MacroMatcherSpec -> package examples
[error] context owners: value $outer -> anonymous class $anonfun -> value <local Test> -> class Test -> method e1 -> class MacroMatcherSpec -> package examples
[error]
[error] == Enclosing template or block ==
[error]
[error] DefDef( // val $outer(): Test.this.type
[error] <method> <synthetic> <stable> <expandedname>
[error] "examples$MacroMatcherSpec$Test$$anonfun$$$outer"
[error] []
[error] List(Nil)
[error] <tpt> // tree.tpe=Any
[error] $anonfun.this."$outer " // private[this] val $outer: Test.this.type, tree.tpe=Test.this.type
[error] )
Run Code Online (Sandbox Code Playgroud)
我真的很难破译这意味着什么,但我怀疑它与我无法this.otherMethod在吸血鬼方法体内引用这一事实有关.有没有办法做到这一点?
如果这样做,我的下一步将是这样的实现otherMethod:
def otherMethod(p: Int) = new $className {
override protected val aValue = p
}
Run Code Online (Sandbox Code Playgroud)
“this”通常作为 c.prefix.tree 提供。所以也许像
val bodyInstance =
q"(p: Int) => ${c.prefix.tree}.otherMethod(p * 2)"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
757 次 |
| 最近记录: |