This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Unexpected behavioural difference between SortedMap ++ when using immutable version

1 reply
Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 3 days ago.
I have just spent the best part of a day in 2.8-Beta1 trying to understand why adding one SortedMap to another SortedMap
threw away the ordering specified by my original Map.

In the below code, if you run it as is: you will get the following output (which I would expect):

   Map(5 -> Foo, 4 -> Bar, 2 -> Hello, 1 -> World)

If, however, you change the import line as I have indicated in orange, the following (unexpected) output occurs (i.e. the resulting map m3 has the default ordering and not that inherited from m1).

    Map(1 -> World, 2 -> Hello, 4 -> Bar, 5 -> Foo)

This is because the immutable.SortedMap trait overrides the ++ method whereas the collection.SortedMap does not (and hence the default CanBuildFrom comes into scope which, in turn, pulls in the default Ordering[Int])

My question is therefore, "is this a bug" or do people think this is reasonable behaviour?

Chris

Here is the small example:

object SMtest {
  def main(args: Array[String]) {

    import scala.collection.immutable.SortedMap //(change to scala.collection.SortedMap)
    import scala.math.Ordering


    val order = implicitly[Ordering[Int]].reverse
    var m1: SortedMap[Int, String] = SortedMap.empty(order)
    var m2: SortedMap[Int, String] = SortedMap.empty(order)

    m1 += (1 -> "World")
    m1 += (2 -> "Hello")

    m2 += (4 -> "Bar")
    m2 += (5 -> "Foo")

    val m3: SortedMap[Int, String] = m1 ++ m2
    println(m3)

  }
}

Get a new e-mail account with Hotmail - Free. Sign-up now.
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Unexpected behavioural difference between SortedMap ++ when

On Mon, Apr 19, 2010 at 05:49:20PM +0000, christopher marshall wrote:
> My question is therefore, "is this a bug" or do people think this is
> reasonable behaviour?

Given that you are in effect upcasting the result (from
immutable.SortedMap to collection.SortedMap) then it's hard to name a
convincing reason it "should" have a different result. But this is only
one example of a major issue with the new collections (and I choose the
word issue not as a synonym for bug or misfeature but merely as
"potential source of difficulties which all should be aware of") and
that is that the expected return type of an expression figures quite
centrally into how exactly the expression will be evaluated.

If you rely on type inference in some given situation, this can lead to
interesting surprises when some change in unrelated code elsewhere has a
ripple effect which alters the expected return type. And if you DON'T
rely on type inference, it can also lead to interesting surprises as in
the example you are supplying here.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland