3

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?

2
  • So far there's no better way unfortunately. Commented Jun 20, 2016 at 12:28
  • If you need to get the values of the parameters, this works: stackoverflow.com/a/42961043/3669757 Commented Mar 22, 2017 at 23:10

1 Answer 1

2

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)"))
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.