“特质”和“特质”有什么区别?和“模板特征”?
查看 Traversable 和 TraversableLike 的 scaladoc,我很难弄清楚它们之间的区别(除了一个扩展了另一个)。文档中唯一明显的区别是,它说 Traversable 是一种“特征”,而 TraversableLike 是一种“模板特征”。但谷歌搜索“模板特征”并没有揭示这个术语的定义。帮助!
Looking at the scaladoc for Traversable and TraversableLike, I'm having a hard time figuring out what the difference between them is (except that one extends the other). The only apparent difference in the documentation is that it says Traversable is a "trait" and TraversableLike is a "template trait". But googling for "template trait" does not reveal a definition for this term. Help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还没有在 Scala 中看到过这个术语的普遍使用,我认为它特定于 Scala 集合 API 的设计。您可以通过阅读Scala 集合的架构 (尤其是“分解常见操作”部分)[1] 和 Scala 集合 SID< /a>. SID 的 §4.2 是相关的,尽管它们在那里被称为“实现特征”:
简而言之,它们的目的既是分离实现以在集合层次结构之外使用(例如,
StringOps
扩展TraversableLike
但不是Traversable
),并分解出以保留集合类型的方式进行常见操作(请参阅 IttayD 的回答以获得更彻底的解释)。我应该指出,除非您要扩展集合层次结构,否则您实际上不需要关心这些类。对于普通使用,请关注
Traversable
、Iterable
、Seq
等特征。如果您是 Scala Collections API 的新手,我建议您从 Scala 2.8 Collection API 文档,然后引用 scaladoc 根据需要。您不能指望通过 scaladoc 获得“全局”。[1] 此链接归功于 mihid
I haven't seen this terminology in general use in Scala, and I think it's specific to the design of the Scala collections API. You can learn more by reading The Architecture of Scala Collections (especially the section on "factoring out common operations")[1] and the Scala collections SID. §4.2 of the SID is relevant, although they're referred to as "implementation traits" there:
In short, their purpose is both to separate implementation for use outside the collections hierarchy (e.g.
StringOps
extendsTraversableLike
but notTraversable
) and to factor out common operations in such a way that the collection type is preserved (see IttayD's answer for a more thorough explanation).I should note that you really don't need to be concerned with these classes unless you are extending the collections hierarchy. For ordinary use, focus on the
Traversable
,Iterable
,Seq
, etc. traits. If you're new to the Scala Collections API, I would suggest starting with the Scala 2.8 Collection API document, then reference the scaladoc as necessary. You can't expect to get the 'big picture' looking through the scaladoc.[1] credit goes to michid for this link
XXXLike 特征对于添加 Repr 泛型参数具有重要作用。应该返回相同集合类型的方法,如filter、map、flatMap,是在低级特征(TraversableLike)中实现的。为了编码它们的返回类型,这些特征接收它:(
对于map和flatMap,问题更复杂,我不会在这里讨论)
现在假设你有一个新的集合类型。您可以这样做:
但是,如果有人想要扩展您的集合,他们就会被各种继承方法的 MyCollection 返回值所困扰。
因此,您可以创建:
并且
任何想要扩展您的集合的人都会扩展 MyCollectionLike
The XXXLike traits have an important role of adding the Repr generic parameter. Methods that are supposed to return the same collection type, like filter, map, flatMap, are implemented in low level traits (TraversableLike). To encode their return type, those traits receive it:
(for map and flatMap the issue is more complicated, I won't go into it here)
Now say you have a new type of collection. You could do:
But then if anyone wants to extend your collection, they are stuck with return values of MyCollection from the various inherited methods.
So instead, you create:
and
and anyone that wants to extend your collection extends MyCollectionLike
[...]Like 类是实际集合类的实现类。从某种意义上说,它们的行为类似于模板实现,其中大多数(如果不是全部)行为都由实际集合类继承。
有关非常详细且易于理解的概述,请阅读 Scala 集合的架构。
The [...]Like classes are implementation classes for the actual collection classes. In a sense they act like template implementations from which most - if not all - behavior is inherited by the actual collection classes.
For a very detailed and approachable overview read The Architecture of Scala Collections.