相关疑难解决方法(0)

与Scala Map类型匹配的模式

想象一下我Map[String, String]在Scala中有一个.

我想匹配地图中的全套键值对.

这样的事情应该是可能的

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"
}
Run Code Online (Sandbox Code Playgroud)

编译器抱怨说:

error: value Map is not a case class constructor, nor does it have an unapply/unapplySeq method

目前什么是模式匹配键值组合的最佳方法Map

scala map pattern-matching

21
推荐指数
3
解决办法
3万
查看次数

标签 统计

map ×1

pattern-matching ×1

scala ×1