Scala警告擦除以防万一

时间:2012-06-12 22:08:37

标签: scala warnings pattern-matching type-erasure case-statement

我在scala函数中有以下模式匹配案例:

def someFunction(sequences: Iterable[Seq[Int]]):Seq[Int] = sequences match{
    case Seq() => Seq(1)
    case _ => ...
    ...
}

我收到以下警告:

warning: non variable type-argument A in type pattern Seq[A] is unchecked since it is eliminated by erasure
case Seq(_) => Seq(1)
        ^
one warning found

这是什么意思?

1 个答案:

答案 0 :(得分:2)

此警告有点虚假,并且不会出现在Scala 2.10上。事实上,我认为这是Scala 2.8的回归(也就是说,它不存在)。

警告的原因是它将Seq(_)解释为Seq(_: Seq[Int]),因为这是sequences的类型参数,然后抱怨它无法保证{{1}因为,在编译时,将被删除。正如我所说,这是虚假的。