Why does scala TreeSet return a SortedSet

Is there a reason the TreeSet.apply method returns a SortedSet rather than a TreeSet ?

The following code will not compile in scala 2.7

 val t:TreeSet[Int] = TreeSet(1,2,3) 
+6
scala treeset
source share
2 answers

The literal answer is that apply() implemented in terms of ++ , which is defined in a SortedSet , and therefore returns a SortedSet . ++ then uses + , which is defined in a TreeSet , so you can return it to a TreeSet if it is critical (although I would not recommend it, as it depends on the implementation and may change over time!).

What do you need from a TreeSet that you cannot get from a SortedSet?

I'm not sure what is the rationale for the design decision, although it seems like it has changed in 2.8.

+5
source share

This was defined as a short appearance of the current scala collection library and is addressed in the updated collection library, which is part of scala 2.8. See http://www.scala-lang.org/sid/3# for details.

+5
source share

All Articles