title: Generic wildcards in variable declarations in Scala
body: <p>In Java I might do this:</p>
  
  <pre><code>class MyClass {
      private List&lt;? extends MyInterface&gt; list;
  
      public void setList(List&lt;MyImpl&gt; l) { list = l; }
  }
  </code></pre>
  
  <p>...assuming that (<code>MyImpl implements MyInterface</code>) of course.</p>
  
  <p>What is the analog for this in <strong>Scala</strong>, when using a <code>Buffer</code>?</p>
  
  <pre><code>import java.lang.reflect._
  import scala.collection.mutable._
  
  class ScalaClass {
     val list:Buffer[MyInterface]  = null
  
     def setList(l: Buffer[MyImpl]) = {
       list = l
     }
  }
  </code></pre>
  
  <p>This (of course) doesn't compile - but how do I declare the <code>list</code> variable in such a way that it does?</p>
  
  <p><strong>EDIT</strong>; I'm adding a bit more. The difference is obviously something to do with the fact that in Java, generics are <em>never covariant in T</em>, whereas in Scala they can be either covariant or not. For example, the Scala class <code>List</code> is covariant in T (and necessarily immutable). Therefore the following <strong>will compile</strong>:</p>
  
  <pre><code>class ScalaClass {
     val list:List[MyInterface]  = null
  
     def setList(l: List[MyImpl]) = {
       list = l
     }
  }
  </code></pre>
  
  <p>I'm still struggling a bit with the compiler error:</p>
  
  <blockquote>
    <p><code>Covariant type T occurs in contravariant position in ...</code></p>
  </blockquote>
  
  <p>For example; this compiler error occurs in the class declaration:</p>
  
  <pre><code>class Wibble[+T] {
    var some: T = _ //COMPILER ERROR HERE!
   }
  </code></pre>
  
  <p>I'm going to ask a separate question...</p>