gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:15:32 25 4
gpt4 key购买 nike

我尝试按照 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 在宏的主体中,而不是在转换后的代码中,因此您会看到输出。

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

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

要么使用quasiquotesdirectly 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
()
""")
}
}

关于scala - Scala宏注解什么时候执行? (宏观天堂),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55491678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com