为什么我的集合模式匹配在 Scala 中失败?

发布于 2024-12-05 13:33:43 字数 331 浏览 1 评论 0原文

我的代码如下,

  val hash = new HashMap[String, List[Any]]
  hash.put("test", List(1, true, 3))
  val result = hash.get("test")
  result match {
    case List(Int, Boolean, Int) => println("found")
    case _ => println("not found")
  }

我希望打印“找到”,但打印“未找到”。我试图匹配任何具有 Int、Boolean、Int 类型三个元素的列表

My code is as follows

  val hash = new HashMap[String, List[Any]]
  hash.put("test", List(1, true, 3))
  val result = hash.get("test")
  result match {
    case List(Int, Boolean, Int) => println("found")
    case _ => println("not found")
  }

I would expect "found" to be printed but "not found" is printed. I'm trying to match on any List that has three elements of type Int, Boolean, Int

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怎樣才叫好 2024-12-12 13:33:43

您正在检查包含伴随对象的列表 Int布尔值。这些与类 Int布尔值

请改用键入模式。

val result: Option[List[Any]] = ...
result match {
  case Some(List(_: Int, _: Boolean, _: Int)) => println("found")
  case _                                      => println("not found")
}

Scala 参考,第 8.1 节描述了您可以使用的不同模式。

You are checking for a list containing the companion objects Int and Boolean. These are not the same as the classes Int and Boolean.

Use a Typed Pattern instead.

val result: Option[List[Any]] = ...
result match {
  case Some(List(_: Int, _: Boolean, _: Int)) => println("found")
  case _                                      => println("not found")
}

Scala Reference, Section 8.1 describes the different patterns you can use.

云仙小弟 2024-12-12 13:33:43

第一个问题是 get 方法返回一个 Option

scala>   val result = hash.get("test")
result: Option[List[Any]] = Some(List(1, true, 3))

因此您需要匹配 Some(List(...)) ,而不是List(...)

接下来,您将再次检查列表是否包含对象 IntBooleanInt,而不是是否包含类型再次为 IntBooleanInt 的对象。

IntBoolean 都是类型和对象同伴。考虑:

scala> val x: Int = 5
x: Int = 5

scala> val x = Int
x: Int.type = object scala.Int

scala> val x: Int = Int
<console>:13: error: type mismatch;
 found   : Int.type (with underlying type object Int)
 required: Int
       val x: Int = Int
                    ^

所以正确的匹配语句是:

case Some(List(_: Int, _: Boolean, _: Int)) => println("found")

The first problem is that the get method returns an Option:

scala>   val result = hash.get("test")
result: Option[List[Any]] = Some(List(1, true, 3))

So you'd need to match against Some(List(...)), not List(...).

Next, you are checking if the list contains the objects Int, Boolean and Int again, not if it contains objects whose types are Int, Boolean and Int again.

Int and Boolean are both types and object companions. Consider:

scala> val x: Int = 5
x: Int = 5

scala> val x = Int
x: Int.type = object scala.Int

scala> val x: Int = Int
<console>:13: error: type mismatch;
 found   : Int.type (with underlying type object Int)
 required: Int
       val x: Int = Int
                    ^

So the correct match statement would be:

case Some(List(_: Int, _: Boolean, _: Int)) => println("found")
厌倦 2024-12-12 13:33:43

以下也适用于 Scala 2.8

List(1, true, 3) match {
  case List(a:Int, b:Boolean, c:Int) => println("found")
  case _ => println("not found")
}

The following also works for Scala 2.8

List(1, true, 3) match {
  case List(a:Int, b:Boolean, c:Int) => println("found")
  case _ => println("not found")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文