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
. 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).
If this is the case, why is this not expressed in the type system by mak开发者_如何学Going Seq[V]
extend Map[Int, V]
and Set[V]
extend 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
. 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
.
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. Requiring that would impose an unnecessary overhead on the implementation.
Besides, a Map[K,V]
is also an 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)]
. 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.
You can construct a similar argument for your suggestion regarding Set
.
Well, if all you care about Seq
and Set
was that, you'd have a point. 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
. 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:
A
Seq
is concerned about knowing in what order its elements are. Conceptually, how would you prepend an element in aMap
? You'd have to renumber all keys!A
Set
is concerned about the presence or absence of an element. How one would model that in aMap
? 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. ASeq
doesn't have arbitrary keys, and aSet
doesn't have arbitrary values.
精彩评论