Is it possible, through pattern matching, to detect the subtype of a class that is not a case class?
I need to use existing Java classes, so I can't declare my own case classes.
Yep! You can pattern match on type, so if you have different cases for different subtypes, you can get the behavior you're looking for:
trait A
class B extends A
class C extends A
def f(a: A) = a match {
case b: B => "a B!"
case c: C => "a C!"
}
f(new B) // a B!
f(new C) // a C!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With