Description
(Seems like this has to be a well-known issue/puzzler but I couldn't find one with superficial searching)
It seems that TreeSet
is not working as expected when it is used as an Iterable.
Welcome to Scala 2.13.1 (OpenJDK 64-Bit Server VM, Java 1.8.0_252).
scala> import scala.collection.immutable.TreeSet
import scala.collection.immutable.TreeSet
// as expected
scala> TreeSet[Long](5,3,2,12,38,2,6,2).map(identity).mkString(", ")
res0: String = 2, 3, 5, 6, 12, 38
// unexpected
scala> (TreeSet[Long](5,3,2,12,38,2,6,2): Iterable[Long]).map(identity).mkString(", ")
res1: String = 5, 6, 38, 2, 12, 3
The underlying assumption is that for all i: Iterable[T]
where the implementing collection is sorted this property should be valid:
i.toList == i.map(identity).toList
Without the constraint "where the implementing collection is sorted", it is not expected to be true because Iterable
can be implemented for unsorted collections.
Above I used the vague phrase "not working as expected" because the behavior is not explicitly specified (TreeSet
does not specify how it implements Iterable
which is a common problem of inheritance) and in absence of a specification it should do the least surprising thing.
Was observed here: spray/spray-json#329