为什么 for 理解中这两个模式匹配之间的行为存在差异?

发布于 2024-10-16 17:22:12 字数 844 浏览 0 评论 0原文

考虑这个 Map[String, Any]

val m1 = Map(("k1" -> "v1"), ("k2" -> 10))

现在让我们编写一个 for

scala> for ((a, b) <- m1) println(a + b)
k1v1
k210

到目前为止一切顺利。

现在让我们指定第二个成员的类型:

scala> for ((a, b: String) <- m1) println(a + b)
k1v1

scala> for ((a, b: Integer) <- m1) println(a + b)
k210

在这里,当我指定类型时,就会进行过滤,这很棒。

现在说我想使用 Array[Any] 来代替:

val l1 = Array("a", 2)

在这里,事情破裂了:

scala> for (v: String <- l1) println(v)
<console>:7: error: type mismatch;
 found   : (String) => Unit
 required: (Any) => ?

我的双重问题是:

  • 为什么第二个匹配过滤器也没有?
  • 有没有办法在第二种情况下表达这种过滤而不使用脏 isInstanceOf

Consider this Map[String, Any]:

val m1 = Map(("k1" -> "v1"), ("k2" -> 10))

Now let's write a for:

scala> for ((a, b) <- m1) println(a + b)
k1v1
k210

So far so good.

Now let's specify the type of the second member:

scala> for ((a, b: String) <- m1) println(a + b)
k1v1

scala> for ((a, b: Integer) <- m1) println(a + b)
k210

Here, as I specify a type, filtering takes place, which is great.

Now say I want to use an Array[Any] instead:

val l1 = Array("a", 2)

Here, things break:

scala> for (v: String <- l1) println(v)
<console>:7: error: type mismatch;
 found   : (String) => Unit
 required: (Any) => ?

My double question is:

  • why doesn't the second match filter as well?
  • is there a way to express such filtering in the second scenario without using a dirty isInstanceOf?

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

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

发布评论

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

评论(2

凌乱心跳 2024-10-23 17:22:12

好吧,后一个例子不起作用,因为它没有被指定。关于什么是合理的行为存在一些讨论。就我个人而言,我希望它能像你一样工作。问题是:

val v: String = (10: Any) // is a compile error
(10: Any) match {
    case v: String =>
} // throws an exception

如果您不相信这一点,请加入俱乐部。 :-) 这是一个解决方法:

for (va @ (v: String) <- l1) println(v)

请注意,在 Scala 3 中,您可以:

for (case v: String <- l1) println(v)

Well, the latter example doesn't work because it isn't spec'ed to. There's some discussion as to what would be the reasonable behavior. Personally, I'd expect it to work just like you. The thing is that:

val v: String = (10: Any) // is a compile error
(10: Any) match {
    case v: String =>
} // throws an exception

If you are not convinced by this, join the club. :-) Here's a workaround:

for (va @ (v: String) <- l1) println(v)

Note that in Scala 3, you can:

for (case v: String <- l1) println(v)
記柔刀 2024-10-23 17:22:12

指定行为的主要原因是为了清晰起见,我们希望鼓励人们添加类型注释。如果为了理解,它们可能会进行成本非常高的过滤操作,这是我们想要避免的陷阱。然而,我同意我们应该更容易地指定某事物是一种模式。也许一对括号就足够了。

val x: String = y       // type check, can fail at compile time
val (x: String) = y     // pattern match, can fail at run time

for (x: String <- ys)   // type check, can fail at compile time
for ((x: String) <- ys) // pattern match, can filter at run time

The main reason for the speced behavior is that we want to encourage people to add type annotations, for clarity. If in for comprehensions, they get potentially very costly filter operations instead, that's a trap we want to avoid. However, I agree that we should make it easier to specify that something is a pattern. Probably a single pair of parens should suffice.

val x: String = y       // type check, can fail at compile time
val (x: String) = y     // pattern match, can fail at run time

for (x: String <- ys)   // type check, can fail at compile time
for ((x: String) <- ys) // pattern match, can filter at run time
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文