Question

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?

Was it helpful?

Solution

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top