user3293336
user3293336

Reputation: 121

How do I resolve a short type name to a full type name in a Macro

Inside a macro is there a way of using the current Context to fully expand a type name? Eg something like:

context.resolveShortTypeNameToFullTypeName("Foo") = "com.acme.Foo"

Upvotes: 2

Views: 776

Answers (1)

som-snytt
som-snytt

Reputation: 39577

Your macro might expand in a tree that includes an arbitrary import prefix.Foo, so you're asking if you can query that enclosing tree: If I emit a name Foo, how would you typecheck it?

symbol.fullName is your answer.

val t = c.typeCheck(q"??? : Foo").tpe.typeSymbol.fullName

or use c.typecheck in 2.11.

or, if you can't find the scaladoc...

val k = c.asInstanceOf[scala.reflect.macros.contexts.Context]
locally {
  import k.universe._
  val n = k.callsiteTyper.typed(q"??? : Foo").tpe.typeSymbol.fullName
  println(n)
}

Where is Travis Brown Eugene Burmakro [sic] when you need him?

Upvotes: 2

Related Questions