Open jackkoenig opened 7 months ago
Unfortunately, this is permissible behavior by design because keys
is not a Seq
for SeqMap
. It's just an Iterable
. Iterables of different types don't have to be compatible with each other--the empty set is not equal to an empty vector, for instance. The proper fix would be to have .keys
be typed as a Seq
.
However, this can still be fixed anyway. It turns out that SeqMap
returns Set
s for .keys
for the specialized small sizes. I think those could be switched to Seq
s with nobody being the wiser for it.
@scala/collections
The Scaladoc was clarified in https://github.com/scala/scala/pull/10544/files
The language of the doc is designed to dissuade from assumptions about keys
or keySet
.
Edit: it looks like keySet
compares equal in this example.
Edit: it looks like keySet compares equal in this example.
If SeqMap
were to follow this deprecatedOverriding
suggestion on keys
("This method should be an alias for keySet")* then the behavior would be as I would expect since as you say, keySet
compares equal.
Regardless, this is permissible behavior. What is the right way to say "do these two SeqMaps have the same keys in the same order?" seqMap1.keysIterator == seqMap2.keysIterator
?
@jackkoenig I see SeqMap doc says order is not relevant for equals.
For keys,
m1.keysIterator.sameElements(m2.keysIterator)
I submitted a PR to tweak keys
to return a Seq just for SeqMap. Other than that, the API remains ambiguous (as to efficiency or what it returns). The keySets of sorted things are sorted, so presumably keySet.toSeq
is also fine.
https://github.com/scala/scala/pull/10766 ended inconclusively. I guess we'll see if anyone else cares enough to pick it up
The previous comment to use sameElements
is enshrined in the doc contributed in the PR.
Reproduction steps
Shows up in Scala 2.13.13 and Scala 3.3.3 (which makes sense as this is the behavior of the standard library)
This prints:
(Also in Scastie form: https://scastie.scala-lang.org/5eMlHcERQsSNQ4mXent1XQ)
Problem
I would expect the
.keys
to be equal forSeqMap
andVectorMap
. Note that we do get the expected behavior for sizes > 4, presumably due to specialization of SeqMap0-4.