“Any”上的模式匹配为2元组?

时间:2015-01-08 04:50:29

标签: scala erasure

给出以下功能:

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元组上进行模式匹配的尝试。

1 个答案:

答案 0 :(得分:1)

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

但你可以:

scala> (("hi",42): Any) match { case (_: String, _: Int) => }