Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting MatchError when using a placeholder for an unused variable

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?

like image 380
himanshuIIITian Avatar asked Sep 02 '20 08:09

himanshuIIITian


Video Answer


1 Answers

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

like image 154
Tomer Shetah Avatar answered Oct 23 '22 01:10

Tomer Shetah