Scala - pattern-matching a tuple of related types

Viewed 29570

I have the following class hierarchy:

class A
class B extends A
class C extends A

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:

class D (one: A, two: A) {

  def work {
    (one, two) match {
      case (o, t): (B, B) => ... blablabla
      case (o, t): (B, C) => ... blablabla
      case _ =>
    }
  }
}

However, when it should resolve the matching in favor of the second case (B, C), it tries resolving it as (B, B) and comes up with the class cast exception that C cannot be cast to B. Why? What to do? How can I come around this?

3 Answers
Related