summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library/scala/collection/SetLike.scala15
-rw-r--r--src/library/scala/collection/mutable/SetLike.scala3
2 files changed, 13 insertions, 5 deletions
diff --git a/src/library/scala/collection/SetLike.scala b/src/library/scala/collection/SetLike.scala
index fbbd77d8aa..5a3b9734ed 100644
--- a/src/library/scala/collection/SetLike.scala
+++ b/src/library/scala/collection/SetLike.scala
@@ -11,6 +11,7 @@ package scala.collection
import generic._
import mutable.{Builder, AddingBuilder}
+import scala.annotation.migration
/** A template trait for sets.
*
@@ -72,6 +73,12 @@ self =>
*/
override protected[this] def newBuilder: Builder[A, This] = new AddingBuilder[A, This](empty)
+ // note: this is only overridden here to add the migration annotation,
+ // which I hope to turn into an Xlint style warning as the migration aspect
+ // is not central to its importance.
+ @migration(2, 8, "Set.map now returns a Set, so it will discard duplicate values.")
+ override def map[B, That](f: A => B)(implicit bf: CanBuildFrom[This, B, That]): That = super.map(f)(bf)
+
/** Tests if some element is contained in this set.
*
* @param elem the element to test for membership.
@@ -182,9 +189,13 @@ self =>
* Unless overridden this is simply `"Set"`.
*/
override def stringPrefix: String = "Set"
-
override def toString = super[IterableLike].toString
- override def hashCode() = this map (_.hashCode) sum
+
+ // Careful! Don't write a Set's hashCode like:
+ // override def hashCode() = this map (_.hashCode) sum
+ // Calling map on a set drops duplicates: any hashcode collisions would
+ // then be dropped before they can be added.
+ override def hashCode() = this.foldLeft(0)(_ + _.hashCode)
/** Compares this set with another object for equality.
*
diff --git a/src/library/scala/collection/mutable/SetLike.scala b/src/library/scala/collection/mutable/SetLike.scala
index 6264d1fbc2..26f19555cd 100644
--- a/src/library/scala/collection/mutable/SetLike.scala
+++ b/src/library/scala/collection/mutable/SetLike.scala
@@ -68,9 +68,6 @@ trait SetLike[A, +This <: SetLike[A, This] with Set[A]]
*/
override protected[this] def newBuilder: Builder[A, This] = empty
- @migration(2, 8, "Set.map now returns a Set, so it will discard duplicate values.")
- override def map[B, That](f: A => B)(implicit bf: CanBuildFrom[This, B, That]): That = super.map(f)(bf)
-
/** Adds an element to this $coll.
*
* @param elem the element to be added