Scala's MapLike, ListLike, SeqLike, etc how does each compare to Map, List, Seq?

前端 未结 1 1534
慢半拍i
慢半拍i 2021-02-12 15:45

Could someone please help me understand Scala\'s various \"Like\" traits in the collection API. I\'ve been reading over and trying to compare each without luck. I think I can se

1条回答
  •  日久生厌
    2021-02-12 16:27

    The best source for these details is Martin Odersky and Lex Spoon's "What's New in Scala 2.8: The Architecture of Scala Collections":

    The Scala collection library avoids code duplication and achieves the "same-result-type" principle by using generic builders and traversals over collections in so-called implementation traits. These traits are named with a Like suffix; for instance, IndexedSeqLike is the implementation trait for IndexedSeq, and similarly, TraversableLike is the implementation trait for Traversable. Collection classes such as Traversable or IndexedSeq inherit all their concrete method implementations from these traits. Implementation traits have two type parameters instead of one for normal collections. They parameterize not only over the collection's element type, but also over the collection's representation type, i.e., the type of the underlying collection, such as Seq[I] or List[T]...

    The whole article is extremely useful if you want to integrate your own collection classes with the Collections API, or if you just want a deeper understanding of how the library works.

    0 讨论(0)
提交回复
热议问题