使用 Scala 2.13.x,我得到 scala.MatchError: null
当我为未使用的变量使用占位符时:
scala> object Test {
| val _: Any = null
| }
object Test
scala> Test
scala.MatchError: null
... 41 elided
但是对于 Scala 2.12.x,我没有得到 scala.MatchError: null
:scala> object Test {
| val _: Any = null
| }
defined object Test
scala> Test
res1: Test.type = Test$@784c5ef5
任何原因?
最佳答案
如 scala 2.13 release notes 中所述:
- 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)- Make extractor patterns null safe. (#6485)
null
is treated as no match.
当将两者结合起来时,我们可以看到这在 Scala 2.13 的设计中是不可能的。有关更多信息,您可以在实现这两个功能的 github 上的拉取请求中阅读:
下划线不再是合法标识符,除非反引号 - https://github.com/scala/bug/issues/10384
使提取器模式为空安全 - https://github.com/scala/scala/pull/6485
关于scala - 为未使用的变量使用占位符时出现 MatchError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63701707/