title: Scala covariance and lower bound i dont get it
body: <p>I am currently learning scala and i am confused about the variance annotations especially the <strong>covariance</strong> and <strong>contravariance</strong>.</p>
  
  <p>So i did some research an came upon following example</p>
  
  <pre><code> class Box[+T] {
  
      def put[U &gt;: T](x: U): List[U] = {
        List(x)
      }
  
    }
  
    class Animal {
  
    }
  
    class Cat extends Animal {
  
    }
  
    class Dog extends Animal {
  
    }
  
    var sb: Box[Animal] = new Box[Cat];
  </code></pre>
  
  <p>So this says class Box is covariant in T whiche means Box[Cat] is a subclass of Box[Animal] since Cat is a subclass of Animal. Sofar i understand this. But when it comes to method parameters my understanding ends. The spec says methods parameters can not be covariant so we have to use this lower bound annotation.</p>
  
  <p>Lets look at the method definiton</p>
  
  <pre><code> def put[U &gt;: T](x: U): List[U] = {
     List(x)
   }
  </code></pre>
  
  <blockquote>
    <p>So [U >: T] says that U must be a superclass of T</p>
  </blockquote>
  
  <p>Trying following code </p>
  
  <pre><code>  var sb: Box[Animal] = new Box[Cat];
    sb.put(new Cat);
  </code></pre>
  
  <p>Works as expected but this drives me nuts</p>
  
  <pre><code>  var sb: Box[Animal] = new Box[Cat];
    sb.put(1);
  </code></pre>
  
  <p>It logically makes no sense to me to put an <strong>INT</strong> into a Box of Animals alltough its correct since <strong>INT</strong> will be resolved to <strong>Any</strong> which is a superclass of <strong>Animal</strong>.</p>
  
  <p><strong>So my question is</strong></p>
  
  <p>How do i have to adapt the code that the <code>put method</code> accepts only subtypes of
  animal? I can not use the upper bound annotation</p>
  
  <pre><code>class Box[+T] {
  
      def put[U &lt;: T](x: U): List[U] = {
        List(x)
      }
  
    }
  </code></pre>
  
  <p>since i get this well known error</p>
  
  <blockquote>
    <p>covariant type T occurs in contravariant position in type</p>
  </blockquote>