title: Scala: Have the type parameter of a collection survive a "collect" when the type parameter is a member type
body: <p>Normally, when collecting all the elements of a sequence that match a particular type, the resulting collection has both the type of the original collection and the type selected for:</p>
  
  <pre><code>trait Foo
  trait Bar
  
  trait Baz {
    // Works
    def def1(foo: Seq[Foo]): Seq[Foo with Bar] = 
      foo collect {case foobar: Bar =&gt; foobar}
  }
  </code></pre>
  
  <p>This even works when the the input type is parameterized by a bounded member type and all I want back is a sequence parameterized by the bound type (not the member type):</p>
  
  <pre><code>trait Baz {
    type memberType &lt;: Foo
  
    // Works
    def2(foo: Seq[memberType]): Seq[Foo with Bar] =
      foo collect {case foobar: Bar =&gt; foobar}
  }
  </code></pre>
  
  <p>However, this fails when I actually want back a sequence parameterized by the member type:</p>
  
  <pre><code>trait Baz {
    type memberType &lt;: Foo
  
    // Fails
    def def3(foo: Seq[memberType]): Seq[memberType with Bar] =
      foo collect {case foobar: Bar =&gt; foobar}
  }
  </code></pre>
  
  <p>Error message:</p>
  
  <pre><code>error: type mismatch;
   found   : Seq[this.Foo with this.Bar]
   required: Seq[Baz.this.memberType with this.Bar]
      foo collect {case foobar: Bar =&gt; foobar}
  </code></pre>
  
  <p>To recover functionality, I can include the member type in the <code>collect</code> call, but that seems redundant given that every element has to match that type because of the signature:</p>
  
  <pre><code>trait Baz {
    type memberType &lt;: Foo
  
    // Works
    def def4(foo: Seq[memberType]): Seq[memberType with Bar] =
      foo collect {case foobar: memberType with Bar =&gt; foobar}
  }
  </code></pre>
  
  <p>Is there a way to define a sequence of member types so that they remember their member types when <code>collect</code>ed?</p>