给出以下功能:
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擦除的理解,即对List[T]
的理解是,在运行时,JVM不知道T
的类型。
请解释为什么以上is unchecked since it is eliminated by erasure
出现在2元组上进行模式匹配的尝试。
答案 0 :(得分:1)
元素类型只是Tuple2的类型参数。
但你可以:
scala> (("hi",42): Any) match { case (_: String, _: Int) => }