gpt4 book ai didi

scala - 惯用的 Scala : Semantic difference between Like and Types?

转载 作者:行者123 更新时间:2023-12-04 08:35:49 25 4
gpt4 key购买 nike

正如这个问题的标题所暗示的那样:我的问题更多是关于形式(惯用惯例)而不是功能。简明扼要地说:

What is the semantic difference between MyCollectionLike and MyCollection?



例如: StringLike 和有什么区别和 StringMapLikeMap .仔细查看 Scala API 文档,我可以看出 XLike通常是 X 的父类(super class)型.但是,除此之外,我不清楚这些抽象层之间的语义差异。

在实践中,如果我正在创建一个新的类/特征,当我为该类选择名称时,理解这种区别会很有帮助。

我的具体问题如下:

I want to create the trait: SurjectiveMap[K, T] which can be mixed-in with either Map[K, Set[T]] or MapLike[K, SetLike[T]]. Given that I do not know the semantic difference between *Like and *, I am not sure which to go with.

最佳答案

与 IFoo 和 Foo、Bar 和 BarImpl 之间的区别相同(除了 TraversableLike 是包含实现的 super 特征):

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.



来自 Scala collections architecture

关于scala - 惯用的 Scala : Semantic difference between <Collection>Like and <Collection> Types?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16824152/

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