从http://docs.scala-lang.org/overviews/quasiquotes/usecases.html粘贴以下宏:
import reflect.macros.Context
import language.experimental.macros
val universe = reflect.runtime.universe; import universe._
import reflect.runtime.currentMirror
import tools.reflect.ToolBox
val toolbox = currentMirror.mkToolBox()
object debug {
def apply[T](x: =>T): T = macro impl
def impl(c: Context)(x: c.Tree) = { import c.universe._
val q"..$stats" = x
val loggedStats = stats.flatMap { stat =>
val msg = "executing " + showCode(stat)
List(q"println($msg)", stat)
}
q"..$loggedStats"
}
}
它在Scala 2.11.1中生成此错误消息:
Q.scala:9: error: macro implementation reference has wrong shape. required:
macro [<static object>].<method name>[[<type args>]] or
macro [<macro bundle>].<method name>[[<type args>]]
def apply[T](x: =>T): T = macro impl
^
我尝试过多种方式改变代码(import reflect.macros.whitebox.Context
,import reflect.macros.blackbox.Context
,将scala.
放在每次导入的开头,使参数始终按名称或一致地通过 - 值macro impl[T]
,删除类型参数macro debug.impl
,将apply
放在impl
之后,等等,但没有成功。我究竟做错了什么?这是进口的东西吗?这些(主要)来自不同的网页,http://docs.scala-lang.org/overviews/quasiquotes/setup.html。
该页面的其他两个示例宏都出现同样的错误:
object Macro {
def apply(x: Int): Int = macro impl
def impl(c: Context)(x: c.Expr[Int]): c.Expr[Int] = { import c.universe._
c.Expr(q"$x + 1")
}
}
object Macro {
def apply(x: Int): Int = macro impl
def impl(c: Context)(x: c.Tree) = { import c.universe._
q"$x + 1"
}
}
答案 0 :(得分:4)
scala Foo.scala
在匿名类中包装您提供给它的代码。结果,如:
import scala.reflect.macros.Context
import scala.language.experimental.macros
object debug {
def apply[T](x: T): T = macro impl
def impl(c: Context)(x: c.Tree): c.Tree = ???
}
变换为:
[[syntax trees at end of parser]] // Test.scala
package <empty> {
object Main extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
def main(args: Array[String]): scala.Unit = {
final class $anon extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
import scala.reflect.macros.Context;
import scala.language.experimental.macros;
object debug extends scala.AnyRef {
def <init>() = {
super.<init>();
()
};
<macro> def apply[T](x: T): T = impl;
def impl(c: Context)(x: c.Tree): c.Tree = $qmark$qmark$qmark
}
};
new $anon()
}
}
}
但是,必须在静态对象或包中定义宏实现,这是错误消息试图说的:
/Users/xeno_by/Projects/211x/sandbox/Test.scala:5: error: macro implementation reference has wrong shape. required:
macro [<static object>].<method name>[[<type args>]] or
macro [<macro bundle>].<method name>[[<type args>]]
def apply[T](x: T): T = macro impl
^
one error found
不幸的是,如果将某些内容放入匿名类中,它就不再是静态的,这意味着scala Foo.scala
- 样式开发与宏不兼容。考虑使用替代品,例如scalac
或sbt
。