gpt4 book ai didi

scala - "trait"和 "template trait"之间有什么区别?

转载 作者:行者123 更新时间:2023-12-03 11:48:28 27 4
gpt4 key购买 nike

查看 Traversable 和 TraversableLike 的 scaladoc,我很难弄清楚它们之间的区别是什么(除了一个扩展另一个)。文档中唯一明显的区别是它说 Traversable 是一个“特征”,而 TraversableLike 是一个“模板特征”。但是谷歌搜索“模板特征”并没有揭示这个术语的定义。帮助!

最佳答案

我还没有看到这个术语在 Scala 中普遍使用,我认为它特定于 Scala 集合 API 的设计。您可以通过阅读 The Architecture of Scala Collections(尤其是“分解常见操作”部分)[1] 和 the Scala collections SID 来了解更多信息。 SID 的 §4.2 是相关的,尽管它们在那里被称为“实现特征”:

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's answer 以获得更全面的解释) .

我应该注意到,除非您要扩展集合层次结构,否则您真的不需要关心这些类。对于普通使用,重点关注 TraversableIterableSeq 等特征。如果您不熟悉 Scala Collections API,我建议从 Scala 2.8 Collection API document 开始,然后根据需要引用 scaladoc。您不能期望通过 scaladoc 获得“大局”。

[1] 此链接归功于 michid

关于scala - "trait"和 "template trait"之间有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4599937/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com