小编ili*_*num的帖子

从Scala宏注释获取参数

所以我在函数上有一个注释(DefDef).此注释具有参数.但是,我对如何从构造函数中获取参数感到困惑.

用法示例:

class TestMacro {
  @Foo(true)
  def foo(): String = ""
  foo
}
Run Code Online (Sandbox Code Playgroud)

这是注释的代码:

class Foo(b: Boolean) extends StaticAnnotation {
  def macroTransform(annottees: Any*) = macro Foo.impl
}

object Foo {
  def impl(c: whitebox.Context)(annottees: c.Tree*): c.Expr[Any] = {
    import c.universe._
    //how do I get value of `b` here???
    c.abort(c.enclosingPosition, "message")
  }
}
Run Code Online (Sandbox Code Playgroud)

annotations scala scala-macros

9
推荐指数
2
解决办法
1497
查看次数

标签 统计

annotations ×1

scala ×1

scala-macros ×1