1
II想了解Scala集合:創建通過添加一個新的集合如下類型集合
class NewColl[V](values:Vector[V],someOtherParams)
extends IndexedSeq[V] with IndexedSeqLike[V, NewColl[V]] {
def fromSeq[V](seq: Seq[V]): NewColl[V] = ...
override def newBuilder[V]: Builder[V, NewColl[V]] =
new ArrayBuffer[V] mapResult fromSeq[V]
}
,但我得到了以下錯誤:
overriding method newBuilder in trait TraversableLike of type => scala.collection.mutable.Builder[V,NewColl[V]]; method newBuilder in trait GenericTraversableTemplate of type => scala.collection.mutable.Builder[V,IndexedSeq[V]] has incompatible type
任何想法?
「map」的結果仍然是IndexedSeq而不是NewColl! – teucer
@teucer,你需要提供一個'CanBuildFrom',參見http://www.scala-lang.org/docu/files/collections-api/collections-impl_5.html。我建議瀏覽/閱讀* Scala Collections的架構*,然後嘗試實現您的收藏。相反,無緣無故更具挑戰性。 – huynhjl