New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TreeMap.zipWithIndex loses the TreeMap's custom ordering #10146
Comments
Imported From: https://issues.scala-lang.org/browse/SI-10146?orig=1 |
@som-snytt said:
|
In Scala 2.13.0 Welcome to Scala 2.13.0 (OpenJDK 64-Bit Server VM, Java 12.0.1).
Type in expressions for evaluation. Or try :help.
scala> import scala.collection.immutable.TreeMap
import scala.collection.immutable.TreeMap
scala> val reversedIntOrdering = implicitly[Ordering[Int]].reverse
reversedIntOrdering: scala.math.Ordering[Int] = scala.math.Ordering$Reverse@3bf3e060
scala> val treeMap = TreeMap(2 -> "a", 3 -> "b", 4 -> "c", 5 -> "d")(reversedIntOrdering)
treeMap: scala.collection.immutable.TreeMap[Int,String] = TreeMap(5 -> d, 4 -> c, 3 -> b, 2 -> a)
scala> treeMap.zipWithIndex.foreach(println)
((5,d),0)
((4,c),1)
((3,b),2)
((2,a),3) |
I don't see how the map could keep its non-default ordering, since the type of the key changes. |
@Jasper-M |
I'm also puzzled by Jasper's remark, in the context of 2.13. If we're returning an |
The previous comment was about 2.12. The signature changed for 2.13, which apparently was a long time ago now.
Also to note we'd normally use |
Eh? Jasper's comment directly references 2.13. In any case, the 2.13 ordering is as desired, so we should close this, I think. @som-snytt but feel free to open a new ticket with your new, somewhat different case |
OK. I thought the fix would be similar for sorted things, but the map case requires defining that the result keeps iteration order. Edit: I fixed my comment, as the signature on I read or misread the comment as "impossible in 2.12, but note in 2.13 the signature changed". But neither here nor there. Neither hair nor Nair. (Saving that for later.) |
Hard to be sure after 4 years, but I think this is what I meant. As in, note that in 2.13 the ordering is as desired because the resulting collection doesn't have a |
When one uses zipWithIndex on a TreeMap, and the latter was created using a non-default ordering, the resulting map again receives the default ordering (coming from the implicit parameter).
The result is something unexpected, from the point of view of the end user: the map's ordering is suddenly lost. An artificial example from REPL which illustrates the issue.
What is even funnier, if the map loses its TreeMap type, the result is "regular" again:
The text was updated successfully, but these errors were encountered: