如何绕过这个scala编译器擦除警告?

如何绕过这个scala编译器擦除警告?,scala,Scala,我试图编译这个scala代码,并得到以下编译器警告 scala> val props: Map[String, _] = | x match { | case t: Tuple2[String, _] => { | val prop = | t._2 match { | case f: Function[_, _] => "hello" | case s:Some[Function[_, _]]

我试图编译这个scala代码,并得到以下编译器警告

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"
                  ^
scala>val-props:Map[String,\uz]=
|x匹配{
|案例t:Tuple2[String,\uz]=>{
|瓦尔道具=
|t.(u)2比赛{
|案例f:函数[u,]=>“你好”
|案例s:Some[函数[_,]]=>“世界”
|案例u=>t.2
|       }
|地图(t.\U 1->道具)
|   }
|大小写=>null
| }
:10:警告:类型模式(String,41;中的非变量类型参数字符串未选中,因为它已通过擦除消除
案例t:Tuple2[String,\uz]=>{
^
:14:警告:类型模式中的非变量类型参数=>\某些[\u=>\]未选中,因为它已通过擦除消除
案例s:Some[函数[_,]]=>“世界”
^
上给出的答案似乎指向了同一个问题。但我无法在这个特定的上下文中推断出解决方案。

使用
case t@(:String,)
而不是
case t:Tuple2[String,]
case s@Some(:Function[u,u])
而不是
case s:Some[Function[,]

Scala无法
匹配类型参数

使用
case t@(u:String,)
而不是
case t:Tuple2[String,]
case s@Some(u:Function[u,u])
而不是
case s:Some[Function[,]


Scala无法对类型参数进行
匹配。

我会这样重写它:

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
}

我会这样重写它:

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
}

感谢您的回复。我喜欢这两种方法。感谢您的回复。我喜欢这两种方法。x可以是任意对象或函数x可以是任意对象或函数Case t:WrappedArray[Tuple2[String,]]=>{--也会生成相同类型的擦除警告。你能告诉我为什么会这样吗?@sobychacko之所以如此,是因为类型擦除:在
jvm
运行时
WrappedArray[(String,Int)]
WrappedArray[String]
是相同的:
WrappedArray[Object]
。有关类型参数的信息仅在编译时可用。有关某些链接和解决方法,请参阅。案例t:WrappedArray[Tuple2[String,]]=>{--也会生成相同类型的擦除警告。您能告诉我为什么会这样吗?@sobychacko之所以如此,是因为类型擦除:在
jvm
运行时
WrappedArray[(String,Int)]
WrappedArray[String]
是相同的:
WrappedArray[Object]
。有关类型参数的信息仅在编译时可用。有关一些链接和解决方法,请参阅。