Auto Unpack in Scala Template

In the following code, I get a compilation error stating that I have a type mismatch in 'x':

val someRef: java.lang.Long = 42L someRef match { case x: Long => println("The answer: " + x) case _ => println("Unknown") } 

How do I get Scala in auto-unbox someRef in a matching statement?

+6
source share
1 answer

The type system does not know about boxing at this level. But he knows that if there is Any , then the Long box really (supposedly) should just be Long (from the AnyVal part of the class inheritance tree). So:

 val someRef: java.lang.Long = 42L (someRef: Any) match { case x : Long => println("The answer is " + x) case _ => println("What answer?") } 
+12
source

Source: https://habr.com/ru/post/899065/


All Articles