What about any case where someone want to apply pattern matching on an entire Map object?
Example (though not compiling) would be:
def main(args: Array[String]) {
val m: Map[String, Int] = Map("a" -> 1, "b" -> 2) //scala.collection.immutable.Map2
m match {
case Map2("a",1,"b",2) => println("matched") //conceptual code line
case _ => println("not matched")
}
}
What are possible reasons preventing the Map2 definition as a case class?
- Case class preventing encapsulation of internal fields? (like
key1andvalue1…) - Case class doesn’t apply with generics type parameters?
- Rare usage of matching an entire Map (making often non-sense)?
One salient reason is that deriving new classes from case classes is deprecated at best, dangerous at worst. In order to allow users (or Scala standard library authors) to derive from these classes, they may not be case classes.