I’m trying to achieve something like this:
def a(b: Any) = {
b match {
case x: Seq[String] => println("x")
}
}
// somewhere else
a(List("b"))
As a result I’d love to see “x” being printed, and I don’t.
Basically I want to match against a type/trait and cover all of the objects whose types derive from/implement such type/trait, with the trait being Seq and the type parameter being known in advance.
Since I’m a scala novice I’m quite stuck, however.
Ideas?
You can’t check against parameterized types because of type erasure. See this question why there will be a warning: Warning about an unchecked type argument in this Scala pattern match?
Another question and its answers tell you how to get around that: How do I get around type erasure on Scala? Or, why can’t I get the type parameter of my collections?
Nevertheless your code works fine, when you don’t check against type parameters: