对未使用的变量使用占位符时出现 MatchError [英] Getting MatchError when using a placeholder for an unused variable
问题描述
在 Scala 2.13.x 中,当我为未使用的变量使用占位符时,我得到 scala.MatchError: null
:
With Scala 2.13.x, I am getting scala.MatchError: null
when I use a placeholder for an unused variable:
scala> object Test {
| val _: Any = null
| }
object Test
scala> Test
scala.MatchError: null
... 41 elided
但是使用 Scala 2.12.x,我没有得到 scala.MatchError: null
:
But with Scala 2.12.x, I am not getting scala.MatchError: null
:
scala> object Test {
| val _: Any = null
| }
defined object Test
scala> Test
res1: Test.type = Test$@784c5ef5
有什么理由吗?
推荐答案
- 下划线不再是合法的标识符,除非反引号(bug#10384)
val _ =
现在是模式匹配(并丢弃该值而不产生警告)
- Underscore is no longer a legal identifier unless backquoted (bug#10384)
val _ =
is now a pattern match (and discards the value without incurring a warning)
null
被视为不匹配.
null
is treated as no match.
将两者结合起来时,我们可以看到 Scala 2.13 的设计不可能做到这一点.有关更多信息,您可以在 github 上实现这两个功能的拉取请求中阅读:
When combining both, we can see that this is not possible by design of Scala 2.13 . For more information you can read about at the pull requests at github implementing both features:
下划线不再是合法标识符,除非反引号 - https://github.com/scala/bug/issues/10384
Underscore is no longer a legal identifier unless backquoted - https://github.com/scala/bug/issues/10384
使提取器模式为空安全 - https://github.com/scala/scala/pull/6485
Make extractor patterns null safe - https://github.com/scala/scala/pull/6485
这篇关于对未使用的变量使用占位符时出现 MatchError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!