Pattern matching against Scala Map type
- by Tom Morris
Imagine I have a Map[String, String] in Scala.
I want to match against the full set of key–value pairings in the map.
Something like this ought to be possible
val record = Map("amenity" -> "restaurant", "cuisine" -> "chinese", "name" -> "Golden Palace")
record match {
case Map("amenity" -> "restaurant", "cuisine" -> "chinese") => "a Chinese restaurant"
case Map("amenity" -> "restaurant", "cuisine" -> "italian") => "an Italian restaurant"
case Map("amenity" -> "restaurant") => "some other restaurant"
case _ => "something else entirely"
}
The compiler complains thulsy:
error: value Map is not a case class constructor, nor does it have an unapply/unapplySeq method
What currently is the best way to pattern match for key–value combinations in a Map?