简体   繁体   English

为什么Seq [V]不扩展Map [Int,V],Set [V]也不扩展Map [V,Bool]?

[英]Why does Seq[V] not extend Map[Int,V] nor does Set[V] extend Map[V,Bool]?

The three immediate subtypes of Iterable are Map , Seq , and Set . Iterable的三个直接子IterableMapSeqSet It seems like—aside from performance issues—a Seq is a map from integers to values, and a Set is a map from values to booleans (true if the value is in the set, false otherwise). 似乎除了性能问题之外 - Seq是从整数到值的映射, Set是从值到布尔值的映射(如果值在集合中则为true,否则为false)。

If this is the case, why is this not expressed in the type system by making Seq[V] extend Map[Int, V] and Set[V] extend Map[V, Boolean] ? 如果是这种情况,为什么不通过使Seq[V]扩展Map[Int, V]Set[V]扩展Map[V, Boolean]来在类型系统中表达?

Well, they sort of do, at least actually common functionality. 嗯,他们有点做,至少实际上是常见的功能。 Seq[B] inherits from Int => B (via PartialFunction[Int, B] ), Map[A, B] inherits from A => B (also via PartialFunction[A, B] ), and Set[A] inherits from A => Boolean . Seq[B]继承自Int => B (通过PartialFunction[Int, B] ), Map[A, B]继承自A => B (也通过PartialFunction[A, B] ),而Set[A]继承自A => Boolean Thus, as far as function application and composition methods are concerned, all three can be used interchangeably. 因此,就功能应用和组合方法而言,所有三种都可以互换使用。 Additionally, they can be used interchangeably as far as traversal goes, as all implement TraversableLike . 此外,就遍历而言,它们可以互换使用,因为它们都实现了TraversableLike

Seeing a sequence as an assignment from integers to elements is only one way to describe what a sequence is. 将序列视为从整数到元素的赋值只是描述序列是什么的一种方式。 There are other ways, and there is no reason why that way of describing a sequence should become canonical. 还有其他方法,没有理由说这种描述序列的方式应该成为规范。 The actual purpose of a sequence is to make a bunch of elements accessible and traversable. 序列的实际目的是使一堆元素可访问和遍历。 A sequence is not required to actually assign integer numbers to the elements. 不需要序列来实际为元素分配整数。 For example, most Stream implementations probably don't have a counter running in parallel to the traversal. 例如,大多数Stream实现可能没有与遍历并行运行的计数器。 Requiring that would impose an unnecessary overhead on the implementation. 要求这将对实施施加不必要的开销。

Besides, a Map[K,V] is also an Iterable[(K,V)] . 此外, Map[K,V]也是Iterable[(K,V)] Following your suggestion, a Seq[A] would also have to be a Map[Int,A] , which would by that also make it an Iterable[(Int,A)] . 根据你的建议, Seq[A]也必须是Map[Int,A] ,这也可以使它成为Iterable[(Int,A)] Since Seq extends Iterable , this would make the Seq[A] both an Iterable[A] and an Iterable[(Int,A)] (and, recursively, an Iterable[(Int,(Int,A))] , Iterable[(Int,(Int,(Int,A)))] , and so on), which is not an allowed way of inheritance in Scala. 由于Seq扩展了Iterable ,这将使Seq[A]既是Iterable[A]又是Iterable[(Int,A)] (并且,递归地, Iterable[(Int,(Int,A))]Iterable[(Int,(Int,(Int,A)))]等等,这不是Scala中允许的继承方式。

You can construct a similar argument for your suggestion regarding Set . 您可以为您关于Set的建议构建一个类似的参数。

Well, if all you care about Seq and Set was that, you'd have a point. 好吧,如果关心SeqSet是,你有一个点。 Myself, I happen to think that's one of the least importants aspects, and one which is already well represented by all of them being functions . 我自己,我碰巧认为这是最重要的方面之一,而且已经很好地代表了所有这些方面的功能

That is, a Map is a function of a key into a value, a Seq is a function of an Int into a value, and a Set is a function of a value into a Boolean . 也就是说, Map是键的值的函数, SeqInt的值的函数, Set是函数的函数的Boolean This property, which you called a "map", is a funciton. 这个属性,你称之为“地图”,是一个功能。 And it is already shared by all three. 它已经被所有三个人共享。

What, in my opinion, Map , Seq and Set are really about are: 在我看来, MapSeqSet的真正Seq是:

  • A Seq is concerned about knowing in what order its elements are. Seq关注的是知道它的元素的顺序。 Conceptually, how would you prepend an element in a Map ? 从概念上讲,您如何在Map元素? You'd have to renumber all keys! 你必须重新编号所有键!

  • A Set is concerned about the presence or absence of an element. Set关注元素的存在与否。 How one would model that in a Map ? 如何在Map建模? It would have to be a map with default value -- not a common map -- and one in which all non-default values are the same! 它必须是具有默认值的地图 - 不是公共地图 - 并且所有非默认值都相同! That is clearly a degenerate behavior, not an abstraction. 这显然是一种堕落的行为,而不是一种抽象。

  • A Map is concerned about mapping arbitrary keys to arbitrary values. Map关注将任意键映射到任意值。 A Seq doesn't have arbitrary keys, and a Set doesn't have arbitrary values. Seq没有任意键, Set没有任意值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM