-1

给定以下功能:

scala> def foo(x: Any) = x match { 
     |   case _: (String, Int) => "foo"
     |   case _                => "bar"
     | }

我收到以下编译时警告:

<console>:8: warning: non-variable type argument String in type pattern (String, Int) is unchecked since it is eliminated by erasure
         case _: (String, Int) => "foo"
                 ^
foo: (x: Any)String

我对 JVM 擦除(即 for List[T])的理解是,在运行时,JVM 不知道T.

请解释为什么上面is unchecked since it is eliminated by erasure出现了在 2 元组上进行模式匹配的尝试。

4

1 回答 1

1

元素类型只是 Tuple2 的类型参数。

但是你可以:

scala> (("hi",42): Any) match { case (_: String, _: Int) => }
于 2015-01-08T09:20:03.587 回答