为什么 for 理解中这两个模式匹配之间的行为存在差异?
考虑这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,后一个例子不起作用,因为它没有被指定。关于什么是合理的行为存在一些讨论。就我个人而言,我希望它能像你一样工作。问题是:
如果您不相信这一点,请加入俱乐部。 :-) 这是一个解决方法:
请注意,在 Scala 3 中,您可以:
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:
If you are not convinced by this, join the club. :-) Here's a workaround:
Note that in Scala 3, you can:
指定行为的主要原因是为了清晰起见,我们希望鼓励人们添加类型注释。如果为了理解,它们可能会进行成本非常高的过滤操作,这是我们想要避免的陷阱。然而,我同意我们应该更容易地指定某事物是一种模式。也许一对括号就足够了。
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.