title: How to pattern match on generic type in Scala?
body: <p>Let's suppose we have a generic class <code>Container</code>:</p>
  
  <pre><code>case class Container[+A](value: A)
  </code></pre>
  
  <p>We then want to pattern match a <code>Container</code> with a <code>Double</code> and a <code>Container</code> of <code>Any</code>:</p>
  
  <pre><code>val double = Container(3.3)  
  var container: Container[Any] = double
  </code></pre>
  
  <p>To do this, we would normally write:</p>
  
  <pre><code>container match {  
    case c: Container[String] =&gt; println(c.value.toUpperCase)
    case c: Container[Double] =&gt; println(math.sqrt(c.value))  
    case _ =&gt; println("_")  
  }
  </code></pre>
  
  <p>However, the compiler gives two warnings, one for each of the first two cases. For example, the first warning says: "non-variable type argument String in type pattern Container[String] is unchecked since it is eliminated by erasure". Because of the erasure, it is impossible during runtime to distinguish between different kinds of containers and the first catch will be matched. As a consequence, container of type <code>Container[Double]</code> will be matched by the first case, which catches <code>Container[String]</code> objects, so <code>toUpperCase</code> method will be called on a <code>Double</code> and a <code>java.lang.ClassCastException</code> will be thrown.</p>
  
  <p>How to match a <code>Container</code> parametrized by a particular type?</p>