5

I want to do a similar thing as Scala Macros: Checking for a certain annotation My annotation looks like:

class extract(val name: String) extends StaticAnnotation

And I'm using it like this:

case class MainClass(@extract("strings") foo: String, bar: Int)

I'm trying to get foo parameter Symbol because it has an @extract annotation:

val extrList = params.map { param: Symbol =>
  param.annotations.collect {
    case extr if extr.tpe  <:< c.weakTypeOf[extract] =>
      val args = extr.scalaArgs
      if (args.size != 1)
        abort("@extract() should have exactly 1 parameter")
      getExtractValue(args.head) -> param
  }
}

The getExtractValue method looks like this:

def getExtractValue(tree: Tree): String = ???

How do I get the value name of the @extract annotation

Update

The Tree I get from scalaArgs seems too be unusable by c.eval()

param: Symbol =>
    param.annotations.collect {
      case ann if ann.tpe <:< c.weakTypeOf[extract] =>
        val args = ann.scalaArgs
        val arg0 = args.head
        val name: String = c.eval(c.Expr(arg0))
        echo(s"args @extract(name = $name)")
        name -> param
    }

Gives the error

[error] exception during macro expansion:
[error] scala.tools.reflect.ToolBoxError: reflective toolbox has failed: cannot
operate on trees that are already typed
[error]         at scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal.
verify(ToolBoxFactory.scala:74)

the full stacktrace points to c.eval (I separated c.eval and c.Expr)

Community
  • 1
  • 1
Jaap
  • 3,081
  • 2
  • 29
  • 50
  • I'd rather not fall back to java annotations, since I'll have to write `@extract(name="strings")` which is longer and doesn't look that nice. – Jaap Jan 03 '14 at 17:20
  • I've also asked the question, but a little different here: https://groups.google.com/d/topic/scala-language/gMlJFJz03V8/discussion – Jaap Jan 04 '14 at 19:33
  • See also: http://stackoverflow.com/questions/20936509/scala-macros-what-is-the-difference-between-typed-aka-typechecked-an-untyped – Jaap Jul 26 '15 at 12:04

1 Answers1

7

In this case:

def getExtractValue(tree: Tree) = tree match {
  case Literal(Constant(str: String)) => str
}
ghik
  • 10,706
  • 1
  • 37
  • 50
  • What if you use a reference to a val member in an object? Would this match still work? – Jaap Jan 04 '14 at 08:26
  • @Jaap No, it won't. You could still try to use `c.eval` though. – ghik Jan 04 '14 at 12:25
  • c.eval doesn't work, I'm going to try the match approach now, I wonder why I can't get a reference to the annotation object itself... – Jaap Jan 04 '14 at 19:07
  • I've published the code with c.eval here: https://github.com/q42jaap/scala-extractor-annotation/tree/scala-c-eval – Jaap Jan 04 '14 at 19:32
  • as Eugene Burmako posted on the scala-languague mailing list, matching on the Tree would be the best solution. I've chosen a different path which doesn't need parameters in the annotation anymore. – Jaap Jan 05 '14 at 17:12
  • @Jaap link is broken. – Hassan Syed Jul 24 '15 at 21:38
  • @HassanSyed I see it is, I've apparently deleted that old branch. Since I've been doing some more macro work, I've never needed c.eval anymore. Matching string literals is realy easy with quasiquotes: `val q"${str: String}" = tree` – Jaap Jul 26 '15 at 12:02
  • Found it: https://github.com/q42jaap/scala-extractor-annotation/commit/35486555d2560d61b656d68b6c8f8bef36942b48 – Jaap Jul 26 '15 at 12:06