类型擦除在哪里发生,什么是远离这种情况的安全方法?

时间:2014-01-30 06:29:07

标签: scala type-conversion scala-2.10

def decode(l: List[(Int, Symbol)]) : List[Symbol] = {
    def foo(r : Int, s: Symbol):List[Symbol] = r match {
      case 0 => Nil
      case x: Int => s::foo(r-1,s)
    }

    l match {
      case Nil => Nil
      case h::tail => foo(h._1 , h._2 ) :+ decode(tail)
    }

}

这里我得到一个编译错误“scala:type mismatch; found:List [Object] required:List [Symbol] case h :: tail => foo(h._1:Int,h._2:Symbol): +解码(尾部)                                                      “

1 个答案:

答案 0 :(得分:1)

这不是类型擦除,简单类型不匹配:

foo(h._1 , h._2 ) :+ decode(tail)

foo的结果为List[Symbol],解码结果为List[Symbol]。现在你试图将List置于List内,不要惊讶于编译器认为在List内存储SymbolsList的唯一方法是给出稍后列出对象(任何)类型。

您很可能只想简单地合并两个列表:

foo(h._1 , h._2 ) ++ decode(tail)
相关问题