Reputation: 170713
Getting Parameters from Scala Macro Annotation explains how to get parameters from a macro annotation. However, if I have several parameters with default values:
class Foo(b: Boolean = false, i: Int = 0) extends StaticAnnotation { ... }
I need to write (based on the answer to that question)
val (b, i) = c.prefix.tree match {
case q"new Foo(..$args)" => ???
}
The logic in ???
seems to become very nasty: I need to handle both positional and named parameters, no simple access to the default values, etc. Is there a way to simplify it?
Upvotes: 3
Views: 282
Reputation: 170713
Well, this is a limited solution, but
// same constructor parameters and defaults as Foo
class FooArgs(b: Boolean = false, i: Int = 0)
val args = c.prefix.tree match {
case q"new Foo(...$args)" => c.eval(c.Expr[FooArgs](q"new some_package.FooArgs(...$args)"))
}
Upvotes: 2