title: Scala Macros: Getting a List of TypeSymbols to be used at runtime
body: <p>Is there a way to return a <code>List</code> of <code>TypeSymbol</code>s for each class under a package using macros?</p>
  
  <p>What I am trying to achieve is to write a macro that gives out something equivalent to this list:</p>
  
  <pre><code>scala&gt; import scala.reflect.runtime.universe._
  import scala.reflect.runtime.universe._
  
  scala&gt; case class MyClass1()
  defined class MyClass1
  
  scala&gt; case class MyClass2()
  defined class MyClass2
  
  scala&gt; val typeSymbols = List(typeOf[MyClass1].typeSymbol, typeOf[MyClass2].typeSymbol)
  typeSymbols: List[reflect.runtime.universe.Symbol] = List(class MyClass1, class MyClass2)
  </code></pre>
  
  <p>Here's my setup:</p>
  
  <p>I have a package named <code>foo</code>, under which these are defined:</p>
  
  <pre><code>trait FooTrait
  
  case class Bar() extends FooTrait 
  
  case class Bar() extends FooTrait
  </code></pre>
  
  <p>Here's my macro that gets all type symbols for the classes under foo that extend <code>FooTrait</code>:</p>
  
  <pre><code>def allTypeSymbols_impl[T: c.WeakTypeTag](c: Context)(packageName: c.Expr[String]) = {
    import c.universe._
  
    // Get package name from the expression tree
    val Literal(Constant(name: String)) = packageName.tree
  
    // Get all classes under given package name
    val pkg = c.mirror.staticPackage(name)
  
    // Obtain type symbols for the classes - implementation omitted
    val types = getTypeSymbols(c.universe)(List(pkg))
  
    // Apply method for List. For easy readability in later applications
    val listApply = Select(reify(List).tree, newTermName("apply"))
  
    val result = types.map {
      t =&gt;
        val typeName = c.Expr[TypeSymbol](Ident(t))
        println(s"Typename: $typeName, $t, ${t.toType}")
  
        reify(typeName.splice).tree
    }
  
    println(s"RESULT: ${showRaw(result)}")
  
    c.Expr[List[reflect.runtime.universe.TypeSymbol]](Apply(listApply, result.toList))
  }
  </code></pre>
  
  <p>The first <code>println</code> prints:</p>
  
  <pre><code>Typename: Expr[c.universe.TypeSymbol](Bar), class Bar, foo.Bar
  Typename: Expr[c.universe.TypeSymbol](Baz), class Baz, foo.Baz
  </code></pre>
  
  <p>The second one prints: </p>
  
  <pre><code>RESULT: List(Ident(foo.Bar), Ident(foo.Baz))
  </code></pre>
  
  <p>But I get this error message:</p>
  
  <pre><code>[error] no type parameters for method any2ArrowAssoc: (x: A)ArrowAssoc[A] exist so that it can be applied to arguments (&lt;notype&gt;)
  [error]  --- because ---
  [error] argument expression's type is not compatible with formal parameter type;
  [error]  found   : &lt;notype&gt;
  [error]  required: ?A
  [error] Note that &lt;none&gt; extends Any, not AnyRef.
  [error] Such types can participate in value classes, but instances
  [error] cannot appear in singleton types or in reference comparisons.
  </code></pre>
  
  <p>What should I do to make this work? I suspect that I have to write something else instead of <code>Ident</code>, but I couldn't figure out what.</p>
  
  <p>Using Scala 2.10.2.</p>
  
  <p>Thanks in advance!</p>