Scala宏注解什么时候执行? (宏观天堂)

When are Scala macro annotations executed? (macro paradise)

我尝试实现 documentation 中描述的 Scala 宏注释示例。我设法在使用它们的实际项目之前编译宏注释,即 @compileTimeOnly("enable macro paradise to expand macro annotations") 没有被触发意味着宏注释在其使用之前被编译。到目前为止一切顺利。

但是,我在实际项目中对某些值进行注释时如下:

@identity val foo: Double = 1.1
@identity val bar: String = "bar"

然后我希望在 运行 主项目(通过之前链接的宏注释示例)时发生以下打印:

(<empty>,List(val foo: Double = 1.1))
(<empty>,List(val bar: String = "bar"))

这是我感到困惑的地方,当我 运行 主项目时打印不会发生。但是,它确实在编译主项目时出现了一秒钟作为警告?

(我使用的是 IntelliJ IDEA 和 Scala 2.12.8)

I managed to compile the macro annotations before the actual project which uses them, i.e., the @compileTimeOnly("enable macro paradise to expand macro annotations") does not get triggered meaning that the macro annotation is compiled before its usage

不,@compileTimeOnly 被触发意味着注释在编译使用它的代码后存在。所以它没有被触发意味着宏在编译过程中已经被执行。由于 println 在宏的主体中,而不是在转换后的代码中,因此您会看到输出。

如果你想在运行项目时打印,你需要修改包含转换代码的return值,即示例中的最后两行:

val outputs = expandees
c.Expr[Any](Block(outputs, Literal(Constant(()))))

要么使用 quasiquotes or directly manipulating ASTs.

未经测试,但使用类似这样的准引号应该可以工作

object identityMacro {
  def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
    import c.universe._
    val inputs = annottees.map(_.tree).toList
    val (annottee, expandees) = inputs match {
      case (param: ValDef) :: (rest @ (_ :: _)) => (param, rest)
      case (param: TypeDef) :: (rest @ (_ :: _)) => (param, rest)
      case _ => (EmptyTree, inputs)
    }
    val stringToPrint = (annottee, expandees).toString
    c.Expr[Any](q"""
    println($stringToPrint)
    $expandees
    ()
    """)
  }
}