“特质”和“特质”有什么区别?和“模板特征”?

发布于 2024-10-10 02:02:16 字数 167 浏览 1 评论 0原文

查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弥枳 2024-10-17 02:02:16

我还没有在 Scala 中看到过这个术语的普遍使用,我认为它特定于 Scala 集合 API 的设计。您可以通过阅读Scala 集合的架构 (尤其是“分解常见操作”部分)[1] 和 Scala 集合 SID< /a>. SID 的 §4.2 是相关的,尽管它们在那里被称为“实现特征”:

集合类(例如 Traversable 或 Vector)从实现特征继承所有具体方法实现。这些特征以 Like 后缀命名;例如 VectorLike 是 Vector 的实现特征,TraversableLike 是 Traversable 的实现特征。

简而言之,它们的目的既是分离实现以在集合层次结构之外使用(例如,StringOps 扩展 TraversableLike 但不是 Traversable),并分解出以保留集合类型的方式进行常见操作(请参阅 IttayD 的回答以获得更彻底的解释)。

我应该指出,除非您要扩展集合层次结构,否则您实际上不需要关心这些类。对于普通使用,请关注 TraversableIterableSeq 等特征。如果您是 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:

Collection classes such as Traversable or Vector inherit all their concrete method implementations from an implementation trait. These traits are named with the Like suffix; for instance VectorLike is the implementation trait for Vector and TraversableLike is the implementation trait for Traversable.

In short, their purpose is both to separate implementation for use outside the collections hierarchy (e.g. StringOps extends TraversableLike but not Traversable) 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

醉态萌生 2024-10-17 02:02:16

XXXLike 特征对于添加 Repr 泛型参数具有重要作用。应该返回相同集合类型的方法,如filter、map、flatMap,是在低级特征(TraversableLike)中实现的。为了编码它们的返回类型,这些特征接收它:(

trait TraversableLike[+A, +Repr] ...
  ...
  def filter(p: A => Boolean): Repr = {

对于map和flatMap,问题更复杂,我不会在这里讨论)

现在假设你有一个新的集合类型。您可以这样做:

 trait MyCollection[+A] extends TraversableLike[A, MyCollection]

但是,如果有人想要扩展您的集合,他们就会被各种继承方法的 MyCollection 返回值所困扰。

因此,您可以创建:

 trait MyCollectionLike[+A, +Repr] extends TraversableLike[A, Repr]

并且

 trait MyCollection[+A] extends MyCollectionLike[A, 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:

trait TraversableLike[+A, +Repr] ...
  ...
  def filter(p: A => Boolean): Repr = {

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

 trait MyCollection[+A] extends TraversableLike[A, MyCollection]

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:

 trait MyCollectionLike[+A, +Repr] extends TraversableLike[A, Repr]

and

 trait MyCollection[+A] extends MyCollectionLike[A, MyCollection]

and anyone that wants to extend your collection extends MyCollectionLike

记忆で 2024-10-17 02:02:16

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文