IntelliJ gives me a hint on a following code:
val l = List(0, "1", 2, "3")
l.foreach{_ match {case xx:Int => println(xx);case _ =>}}
The hint is "Convert match statement to partial function"
When I change the foreach to
l.foreach{case x:Int => println(x)}
I get the scala.MatchError
exception. I can use collect
instead of foreach
, however that produces a resulting List
which is never used.
Is there some common way how to handle this (something like foreach ignoring the non-matched values), or should I just ignore the hint?
Put default case back:
val l = List(0, "1", 2, "3")
l.foreach { case xx:Int => println(xx); case _ => }
IDEA will be happy:
In fact, that is what IDEA will generate if you tap proposed action (ALT+ENTER when your caret points to yellowed text)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With