Skip to content
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

SortedSet + operator replaces existing element #5986

Closed
scabug opened this issue Jun 26, 2012 · 1 comment
Closed

SortedSet + operator replaces existing element #5986

scabug opened this issue Jun 26, 2012 · 1 comment
Assignees
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Jun 26, 2012

  • operator in SortedSet doesn't work as stated in Scaladoc:
    "Creates a new set with an additional element, unless the element is already present."

It replaces existing element.

Same thing with ++.

Reproduced in 2.10.0-M4 and 2.9.1.

Example:

Welcome to Scala version 2.10.0-M4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_31).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.collection.immutable.SortedSet
import scala.collection.immutable.SortedSet

scala> class Foo(val name: String, val n: Int) {
     | override def equals(obj: Any): Boolean = obj match { case other: Foo => name == other.name; case _ => false }
     | override def hashCode = name.##
     | override def toString = "Foo(" + name + ", " + n + ")"
     | }
defined class Foo

scala> implicit val ordering: Ordering[Foo] = Ordering.fromLessThan[Foo] { (a, b) => a.name.compareTo(b.name) < 0 }
ordering: Ordering[Foo] = scala.math.Ordering$$anon$9@32370bf9

scala> SortedSet(new Foo("bar", 1)) + new Foo("bar", 2)
res0: scala.collection.immutable.SortedSet[Foo] = TreeSet(Foo(bar, 2))

Another example:

Welcome to Scala version 2.10.0-M4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_31).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.collection.immutable.TreeSet
import scala.collection.immutable.TreeSet

scala> import scala.collection.immutable.SortedSet
import scala.collection.immutable.SortedSet

scala> val s: SortedSet[String] = TreeSet()
s: scala.collection.immutable.SortedSet[String] = TreeSet()

scala> s + new String("foo")
res0: scala.collection.immutable.SortedSet[String] = TreeSet(foo)

scala> res0.head.hashCode
res1: Int = 101574

scala> System.identityHashCode(res0.head)
res2: Int = 1965496749

scala> System.identityHashCode(res0 + new String("foo"))
res3: Int = 856318696

scala> System.identityHashCode(res0 + new String("foo"))
res4: Int = 143836998

scala> System.identityHashCode((res0 + new String("foo")).head)
res5: Int = 1652856443
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Jun 26, 2012

Imported From: https://issues.scala-lang.org/browse/SI-5986?orig=1
Reporter: Patrik Nordwall (patriknw)

@scabug scabug closed this Jun 27, 2012
@scabug scabug added this to the 2.10.0-M4 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.