title: Scala - pattern-matching a tuple of related types
body: <p>I have the following class hierarchy:</p>
  
  <pre><code>class A
  class B extends A
  class C extends A
  </code></pre>
  
  <p>then, there is another class which takes instances of these classes and there is a method, in which two cases of pattern-matching are possible like this:</p>
  
  <pre><code>class D (one: A, two: A) {
  
    def work {
      (one, two) match {
        case (o, t): (B, B) =&gt; ... blablabla
        case (o, t): (B, C) =&gt; ... blablabla
        case _ =&gt;
      }
    }
  }
  </code></pre>
  
  <p>However, when it should resolve the matching in favor of the second case <code>(B, C)</code>, it tries resolving it as <code>(B, B)</code> and comes up with the class cast exception that <code>C cannot be cast to B</code>. Why? What to do? How can I come around this? </p>