Reputation: 5904
I am trying to compile this scala code and getting the following compiler warnings.
scala> val props: Map[String, _] =
| x match {
| case t: Tuple2[String, _] => {
| val prop =
| t._2 match {
| case f: Function[_, _] => "hello"
| case s:Some[Function[_, _]] => "world"
| case _ => t._2
| }
| Map(t._1 -> prop)
| }
| case _ => null
| }
<console>:10: warning: non-variable type argument String in type pattern (String, _) is unchecked since it is eliminated by erasure
case t: Tuple2[String, _] => {
^
<console>:14: warning: non-variable type argument _ => _ in type pattern Some[_ => _] is unchecked since it is eliminated by erasure
case s:Some[Function[_, _]] => "world"
^
The answers given on How do I get around type erasure on Scala? Or, why can't I get the type parameter of my collections? seems to point to this same issue. But I couldn't infer a solution in this particular context.
Upvotes: 0
Views: 742
Reputation: 11244
I would rewrite it like this:
x match {
case (name, method) => {
val prop =
method match {
case f: Function[_, _] => "hello"
case Some(f: Function[_, _]) => "world"
case other => other
}
Map(name -> prop)
}
case _ => null
}
Upvotes: 2
Reputation: 38045
Use case t @ (_: String, _)
instead of case t: Tuple2[String, _]
and case s @ Some(_: Function[_, _])
instead of case s:Some[Function[_, _]]
.
Scala cannot match
on type parameters.
Upvotes: 4