9

より大きな例から抽出された次のマクロは、への参照だけでツリーを作成することになっています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>")).

4

1 に答える 1

10

目的を達成するには、次の 2 つのオプションがあります。

1) を使用しThis(tpnme.EMPTY)ます。現在、これはコンパイルされないため、This(newTypeName(""))代わりに使用する必要がありますが、RC1 ではこれが修正されます。

2) を使用しThis(c.enclosingClass.symbol.asModule.moduleClass)ます。現在、これはhttps://issues.scala-lang.org/browse/SI-6394のために機能しませんが、RC1 では修正される予定です。

于 2012-09-18T07:09:21.590 に答える