“特质"和“特质"有什么区别?和“模板特征"? [英] what is the difference between a "trait" and a "template trait"?

查看:57
本文介绍了“特质"和“特质"有什么区别?和“模板特征"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 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!

推荐答案

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

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:

诸如 Traversable 或 Vector 之类的集合类从一个实现特征继承了它们所有的具体方法实现.这些特征以 Like 后缀命名;例如 VectorLike 是 Vector 的实现特征,而 TraversableLike 是 Traversable 的实现特征.

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.

简而言之,它们的目的是分离实现以供集合层次结构之外使用(例如 StringOps extends TraversableLike 但不是 Traversable)和以保留集合类型的方式提取常见操作(请参阅 IttayD 的回答 以获得更详尽的解释).

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

我应该注意,除非您要扩展集合层次结构,否则您真的不需要关心这些类.对于普通使用,重点关注 TraversableIterableSeq 等特性.如果您是 Scala Collections API 的新手,我建议您从 Scala 2.8 Collection API 文档,然后参考 scaladoc 根据需要.您不能期望通过 Scaladoc 获得大局".

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] 此链接归功于 michid

[1] credit goes to michid for this link

这篇关于“特质"和“特质"有什么区别?和“模板特征"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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