Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert match statement to partial function when foreach is used

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?

like image 461
Suma Avatar asked May 20 '14 12:05

Suma


1 Answers

Put default case back:

val l = List(0, "1", 2, "3")

l.foreach { case xx:Int => println(xx); case _ => }

IDEA will be happy:

enter image description here

In fact, that is what IDEA will generate if you tap proposed action (ALT+ENTER when your caret points to yellowed text)

like image 121
om-nom-nom Avatar answered Oct 13 '22 07:10

om-nom-nom