Pattern matching against Scala Map type

Posted by Tom Morris on Stack Overflow See other posts from Stack Overflow or by Tom Morris
Published on 2012-11-23T22:56:06Z Indexed on 2012/11/24 11:06 UTC
Read the original article Hit count: 115

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about scala

Related posts about map