title: In Scala, how do I build a factory for nested types, outside of the containing class?
body: <p>Explained with an example:</p>
  
  <pre><code>class A {
      def f = {
          val b: B = C.factory(this).asInstanceOf[B]
      }
      class B
  }
  
  object C {
      def factory(a: A): A#B = new a.B
  }
  </code></pre>
  
  <p><code>C.factory</code> is a function for creating a new instance of an <code>A#B</code>. Since <code>B</code> is a nested type, I've included an <code>A</code> reference in the function signature. The calling function <code>f</code> has to cast the reference returned from <code>factory</code>, which I'd like to avoid. Without the cast I get:</p>
  
  <pre><code>error: type mismatch;
  found   : A#B
  required: A.this.B
  </code></pre>