[英]Where does the type erasure happen and what is a safe way to stay away from this?
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)
}
}
Here I get a compilation error "scala: type mismatch; found:List[Object] required: List[Symbol] case h::tail => foo(h._1 : Int, h._2 : Symbol) :+ decode(tail) " 在这里,我得到了一个编译错误“ scala:类型不匹配;找到了:List [Object]必需:List [Symbol] case h :: tail => foo(h._1:Int,h._2:Symbol):+ decode(tail )“
It is not a type erasure, simple type mismatch: 这不是类型擦除,简单的类型不匹配:
foo(h._1 , h._2 ) :+ decode(tail)
Result of foo is List[Symbol]
, result of decode is List[Symbol]
. foo的结果是
List[Symbol]
,解码的结果是List[Symbol]
。 Now you're trying to put List
inside List
, no surprize that compiler thinks that the only way to store List
and Symbols
inside List
is to give that later list Object (Any) type. 现在你试图把
List
里面List
,没有surprize该编译器认为,只有这样,才能存储List
和Symbols
内部List
是给后来列表对象(任意)类型。
Most likely you wanted to simply combine two lists: 您很可能只想合并两个列表:
foo(h._1 , h._2 ) ++ decode(tail)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.