Getting MatchError when using a placeholder for an unused variable

泪湿孤枕 提交于 2021-01-23 04:49:37

问题


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

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

Any reason?


回答1:


As stated in 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.

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:

Underscore is no longer a legal identifier unless backquoted - https://github.com/scala/bug/issues/10384

Make extractor patterns null safe - https://github.com/scala/scala/pull/6485



来源:https://stackoverflow.com/questions/63701707/getting-matcherror-when-using-a-placeholder-for-an-unused-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!