Is there a way to return a List
of TypeSymbol
s for each class under a package using macros?
What I am trying to achieve is to write a macro tha
You have to use reifyType to create reflection artifacts in the runtime universe:
import scala.language.experimental.macros
import scala.reflect.macros.Context
object PackageMacros {
def allTypeSymbols[T](packageName: String) = macro allTypeSymbols_impl[T]
def allTypeSymbols_impl[T: c.WeakTypeTag](c: Context)(
packageName: c.Expr[String]
) = {
import c.universe._
val pkg = packageName.tree match {
case Literal(Constant(name: String)) => c.mirror.staticPackage(name)
}
val types = pkg.typeSignature.members.collect {
case sym: ClassSymbol =>
c.reifyType(treeBuild.mkRuntimeUniverseRef, EmptyTree, sym.toType)
}.toList
val listApply = Select(reify(List).tree, newTermName("apply"))
c.Expr[List[Any]](Apply(listApply, types))
}
}
This will give you a list of type tags, not symbols, but you can pretty easily get the symbols, either like this:
scala> PackageMacros.allTypeSymbols("foo").map(_.tpe.typeSymbol) foreach println
class Baz$
class Bar
class Baz
trait FooTrait
class Bar$
Or in the macro itself.