Scala的MapLike,ListLike,SeqLike等,它们如何与Map,List,Seq比较? [英] Scala's MapLike, ListLike, SeqLike, etc how does each compare to Map, List, Seq?

查看:913
本文介绍了Scala的MapLike,ListLike,SeqLike等,它们如何与Map,List,Seq比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我理解Scala在集合API中的各种Like特性。我一直在阅读,并试图比较每一个没有运气。我想我可以看到Map为例,扩展了MapLike - 添加了2个具体方法。但这提出了为什么这样做的问题?为什么不在Collection API而不是Map和MapLike中有1个Map trait?

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 see that Map for example, extends MapLike - adding 2 concrete methods. But this begs the question of why do this at all? Why not just have 1 Map trait in the Collections API instead of Map and MapLike?

谢谢!

推荐答案

这些细节的最佳来源是 Martin Odersky和Lex Spoon的Scala 2.8中的新功能:Scala集合的架构

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":


Scala集合库避免代码重复,并通过使用通用构建器和遍历
在所谓的实现特性中的集合实现
same-result-type原理。这些traits
命名为 Like 后缀;例如 IndexedSeqLike IndexedSeq
实现特性,类似地,
TraversableLike Traversable 的实现特性。
集合类如 Traversable IndexedSeq 继承所有
从这些特性的具体方法实现。
实现traits有两个类型参数,而不是
正常集合中的一个。它们不仅在集合的
元素类型上,而且在集合的表示类型
之间,参数化底层集合的类型,例如 Seq [I] 列表[T] ...

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]...

如果您想将自己的集合类与Collections API集成,或者只想更深入地了解库的工作原理,整篇文章是非常有用的。

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.

这篇关于Scala的MapLike,ListLike,SeqLike等,它们如何与Map,List,Seq比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆