I have success when the annotation's argument is a literal, like:

@Annotation(2)
class AnnotatedClass

since I can get the value in the macro's impl with:

c.prefix.tree match {
  case Apply(_, List(Literal(Constant(x)))) => x.toInt
}

But I'm stumped when the annotation's argument is not a literal, like:

object Obj {val n = 2}

@Annotation(Obj.n)
class AnnotatedClass

Similar to the false-start in this question, I can match c.prefix.tree again and pull out the names Obj and n, but how do I get the value of Obj.n?

有帮助吗?

解决方案

Unfortunately, at the moment it's virtually impossible. Evaluation of trees is essentially not supported by our macro system (we have c.eval, but its current implementation is slow and only works reliably with literal arguments).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top