想象一下,我在斯卡拉有一個Map[String, String]
。與斯卡拉的模式匹配地圖類型
我想匹配地圖中的全套鍵值對。
像這樣的東西應該是可能的
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"
}
編譯器會抱怨thulsy:
error: value Map is not a case class constructor, nor does it have an unapply/unapplySeq method
目前什麼是對一個Map
鍵值組合模式匹配的最佳方法是什麼?
嵌套匹配的解決方案看起來相當不錯。 –