Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unchecked type pattern" warning in Scala?

Suppose I have a map m: Map[Any, Int]. Now I would like to take only entries (String, Int) from m and create a new map m1: Map[String, Int] with those entries.

I am trying to do the following:

val m1: Map[String, Int] = m collect {case e:(String, Int) => e}

It seems working but I get a warning: non variable type-argument String in type pattern (String, Int) is unchecked since it is eliminated by erasure.

How can I get rid of the warning?

like image 868
Michael Avatar asked Apr 24 '12 13:04

Michael


2 Answers

you probably want:

val m1: Map[String, Int] = m collect {case (k:String, v:Int) => k->v}
like image 200
virtualeyes Avatar answered Sep 28 '22 08:09

virtualeyes


(Just for reference. What you want is virtualeyes’s answer.)

val m1: Map[String, Int] = m flatMap { e =>
  e._1 match {
    case e1: String => Some(e1 -> e._2)
    case _ => None
  }
}
like image 41
Debilski Avatar answered Sep 28 '22 07:09

Debilski