【发布时间】:2012-11-23 22:56:06
【问题描述】:
假设我在 Scala 中有一个 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 中的键值组合进行模式匹配的最佳方式是什么?
【问题讨论】:
标签: scala map pattern-matching