![](/img/trans.png)
[英]How to suppress intellij IDEA error in editor when using Binding.scala macro annotation?
[英]Using Scala Macro Annotation in Intellij
我正在尝试使用Scala宏注释来简单地打印方法的返回值。 我正在使用Intellij2017。代码如下:
class PrintResult extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro PrintResult.impl
}
object PrintResult {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
val result = {
annottees.map(_.tree).toList match {
case q"$mods def $methodName(...$args): $returnType = { ..$body }" :: Nil => {
q"""$mods def $methodName(...$args): $returnType = {
val res = {..$body}
println($res)
res
}"""
}
case _ => c.abort(c.enclosingPosition, "Annotation @PrintResult can be used only with methods")
}
}
c.Expr[Any](result)
}
}
@PrintResult
object Test extends App {
def add(a: Int, b: Int): Int = {
a+b
}
}
我将此配置添加到sbt:
addCompilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)
我还将SBT Compile添加到了Intellij Run Configuration。
我收到此错误:
macro annotation could not be expanded (the most common reason for that is that you need to enable the macro paradise plugin; another possibility is that you try to use macro annotation in the same compilation run that defines it)
引用错误消息:
另一种可能性是您尝试在定义宏的同一编译运行中使用宏注释
object Test
必须位于其他子项目中或src/test/scala
(当PrintResult
位于src/main/scala
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.