In a test call a third-party function which returns an Option[Any]
. I know if this function returns a Map
, it is a Map[String, Any]
. In this case, I want to check for individual map elements.
theFunction(...) match {
case Some(m: Map[String, Any]) =>
m("some key") match {
case some_condition => ... (my check)
}
case _ => fail("Invalid type")
}
But the compiler warns that case Some(m: Map[String, Any])
is unchecked. When I use Map[_,_]
instead, the compiler bails out at the point where I check m("some key")
.
How do I suppress this warning, or better: how do I do this check correctly? The only approach I can think of is something like
theFunction(...) match {
case Some(m: Map[_,_]) =>
val m1: Map[String, Any] = m.toSeq.map(t => t._1.asInstanceOf[String] -> t._2).toMap
m1("some key") match {
case some_condition => ... (my check)
}
}
but in my eyes, this looks ugly and introduces unnecessary converting of the map to a Seq and vice-versa.