title: Path-dependent types and nested traits
body: <h2>Background</h2>
  
  <p>Suppose I have some nested traits:</p>
  
  <pre><code>trait Foo { trait Bar }
  </code></pre>
  
  <p>And a couple of instances:</p>
  
  <pre><code>val myFoo = new Foo {}
  val myBar = new myFoo.Bar {}
  </code></pre>
  
  <p>I can write the following, which look (at least at a glance) like they should do more or less the same thing:</p>
  
  <pre><code>def whatever1(foo: Foo)(bar: foo.Bar) = bar
  def whatever2(foo: Foo): foo.Bar =&gt; foo.Bar = { bar =&gt; bar }
  def whatever3(foo: Foo) = new { def apply(bar: foo.Bar) = bar }
  case class whatever4(foo: Foo) { def apply(bar: foo.Bar) = bar }
  case class whatever5[F &lt;: Foo](foo: F) { def apply(bar: foo.Bar) = bar }
  </code></pre>
  
  <p>Note that the last is inspired by the solution given <a href="https://groups.google.com/d/msg/scala-internals/p_qcbCp9Pkg/Pmq5pIqsojwJ" rel="nofollow noreferrer">here</a>.</p>
  
  <p>The first three work:</p>
  
  <pre><code>scala&gt; val sameBar1: myFoo.Bar = whatever1(myFoo)(myBar)
  sameBar1: myFoo.Bar = $anon$1@522f63e7
  
  scala&gt; val sameBar2: myFoo.Bar = whatever2(myFoo)(myBar)
  sameBar1: myFoo.Bar = $anon$1@522f63e7
  
  scala&gt; val sameBar3: myFoo.Bar = whatever3(myFoo)(myBar)
  sameBar2: myFoo.Bar = $anon$1@522f63e7
  </code></pre>
  
  <p>But not the fourth or fifth:</p>
  
  <pre><code>scala&gt; val sameBar4: myFoo.Bar = whatever4(myFoo)(myBar)
  &lt;console&gt;:12: error: type mismatch;
   found   : myFoo.Bar
   required: _1.foo.Bar where val _1: whatever4
         val sameBar4: myFoo.Bar = whatever4(myFoo)(myBar)
                                                    ^
  </code></pre>
  
  <p>Fair enough—we can't do the following, either, probably for similar reasons:</p>
  
  <pre><code>scala&gt; val myOof = myFoo
  myOof: Foo = $anon$1@39e4ff0c
  
  scala&gt; val myOofBar: myOof.Bar = new myFoo.Bar {}
  &lt;console&gt;:10: error: type mismatch;
   found   : myFoo.Bar
   required: myOof.Bar
         val myOofBar: myOof.Bar = new myFoo.Bar {}
                                   ^
  </code></pre>
  
  <p>And it's not a big deal, anyway, since we've got three working solutions.</p>
  
  <h2>The problem</h2>
  
  <p>(I'll start by noting that while I first ran into the problem below while working with macros, and while my example here involves the reflection API, my question is not specific to macros or reflection.)</p>
  
  <p>Suppose that I'm working with the new reflection API and want to be able to write the following:</p>
  
  <pre><code>applier[List[_]](Literal(Constant(42)), Literal(Constant(13)))
  </code></pre>
  
  <p>And have it mean something like "give me the abstract syntax tree for <code>List(42, 13)</code>". This isn't too hard—I can just use the approach from <code>whatever3</code> above:</p>
  
  <pre><code>trait ReflectionUtils {
    import scala.reflect.api.Universe
   
    def companionApplier(u: Universe) = new {
      def apply[A: u.TypeTag](xs: u.Tree*): u.Tree = u.Apply(
        u.Select(u.Ident(u.typeOf[A].typeSymbol.companionSymbol), "apply"),
        xs.toList
      )
    }
  }
  </code></pre>
  
  <p>And now I get the syntax I want in my macros (see my <a href="https://gist.github.com/travisbrown/5136824" rel="nofollow noreferrer">answer</a> to <a href="https://stackoverflow.com/q/15329027/334519">this question</a> for a more detailed and motivated example):</p>
  
  <pre><code>object MacroExample extends ReflectionUtils {
    import scala.language.experimental.macros
    import scala.language.reflectiveCalls
    import scala.reflect.macros.Context
  
    def threeOfThem(n: Int) = macro threeOfThem_impl
    def threeOfThem_impl(c: Context)(n: c.Expr[Int]) = {
      val applier = companionApplier(c.universe)
  
      c.Expr[List[Int]](applier[List[_]](n.tree, n.tree, n.tree))
    }
  }
  </code></pre>
  
  <p>And everything works as intended. I don't really like the "reflective access of structural type member" business, though. Unfortunately I can't use the <code>whatever1</code> or <code>whatever2</code> approaches here, since I can't fix the type parameter when I apply this thing to my universe. I'd love to be able to write the following:</p>
  
  <pre><code>case class companionApplier(u: Universe) {
    def apply[A: u.TypeTag](xs: u.Tree*): u.Tree = u.Apply(
      u.Select(u.Ident(u.typeOf[A].typeSymbol.companionSymbol), "apply"),
      xs.toList
    )
  }
  </code></pre>
  
  <p>But this of course runs me into the type mismatch problems we saw with <code>whatever4</code> above.</p>
  
  <p>Is there some other trick I'm missing? Is it possible for me to get the syntax I want without using an anonymous class with a structural type member?</p>