Julian Peeters
Julian Peeters

Reputation: 853

How can I pass an argument to a Scala Macro Annotation impl?

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?

Upvotes: 3

Views: 348

Answers (1)

Eugene Burmako
Eugene Burmako

Reputation: 13048

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

Upvotes: 6

Related Questions