I would like to cast scala collection types, such as Seq[Any]
to Seq[(String, String)]
, without it producing a Warning.
Example code:
val seqs: Seq[Any] = Seq("1" -> "a", "2" -> "b")
def func(seqs: Seq[(String, String)]): String = {
}
func(seqs.asInstanceOf[Seq[(String, String)]]) // this will be warning
EDIT on 2018-10-18:
For a better understanding my question, here is my real case:
I have a function to process something with a parameter Seq[Any]
, actually, I wish this parameter's type is Seq[Int]
or Seq[(String, String)]
:
def getColumns(specifiedSegs: Seq[Any] = Seq.empty): Set[(String, String)] = {
if (specifiedSegs.isEmpty) {
// load all kvs from api
loadAllFromMetaApi() // this will return a Set[(String, String)]
} else {
specifiedSegs.head match {
case _: Int => ... // let's omission this
case _: (String, String) => specifiedSegs.asInstanceOf[Seq[(String, String)]].toSet // warning!
}
}
}
and when I build the project, it prints the warning on specifiedSegs.asInstanceOf[Seq[(String, String)]].toSet
:
warning: non-variable type argument String in type pattern (String, String) is unchecked since it is eliminated by erasure