问题
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