是否存在将SeqView强制为Seq的通用方法?
在Scala 2.12中,SeqView扩展了Seq,因此当您可能需要严格的集合时,您可能会变得懒惰。
我试图将SeqViews强制转换为Seqs,但是遇到了我不熟悉的类型错误:
import scala.collection.SeqView
def force(xs: Seq[Int]): Seq[Int] = xs match {
case view: SeqView[_,_] => view.force
case other => other
}
force(List(1,2,3))
force(List(1,3).view)
Cannot construct a collection of type That with elements of type _ based on a collection of type _.
Scastie链接:https://scastie.scala-lang.org/ThMW8jixT7Odet17EiDavA
值得注意的是,我可以为比赛提供类型参数,但是,当然,我会收到类型擦除警告
import scala.collection.SeqView
def force(xs: Seq[Int]): Seq[Int] = xs match {
case view: SeqView[Int,Seq[Int]] => view.force
case other => other
}
force(List(1,3).view)
类型删除警告使我感到恐惧,但这似乎起作用。
有人能对此有所启发吗?