Skip to content

Commit 0ca92c6

Browse files
committed
Adapt intersect and diff for covariant Set
Note: it would be cleaner to use a bounded wildcard: def intersect(that: Set[_ >: A]): C Unfortunately that would result in worse type inference, see scala/bug#10920
1 parent 9e2bd6f commit 0ca92c6

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/library/scala/collection/Set.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ trait SetOps[+A, +CC[+X] <: SetOps[X, CC, _] with Set[X], +C <: SetOps[A, CC, C]
135135
* @return a new set consisting of all elements that are both in this
136136
* set and in the given set `that`.
137137
*/
138-
def intersect(that: Set[A @uV]): C = this.filter(that)
138+
def intersect[A1 >: A](that: Set[A1]): C = this.filter(that)
139139

140140
/** Alias for `intersect` */
141-
@`inline` final def & (that: Set[A @uV]): C = intersect(that)
141+
@`inline` final def & [A1 >: A](that: Set[A1]): C = intersect(that)
142142

143143

144144
/** Computes the difference of this set and another set.
@@ -147,13 +147,13 @@ trait SetOps[+A, +CC[+X] <: SetOps[X, CC, _] with Set[X], +C <: SetOps[A, CC, C]
147147
* @return a set containing those elements of this
148148
* set that are not also contained in the given set `that`.
149149
*/
150-
def diff(that: Set[A @uV]): C = this.filterNot(that)
150+
def diff[A1 >: A](that: Set[A1]): C = this.filterNot(that)
151151

152152
/** Alias for `diff` */
153-
@`inline` final def &~ (that: Set[A @uV]): C = this diff that
153+
@`inline` final def &~ [A1 >: A](that: Set[A1]): C = this diff that
154154

155155
@deprecated("Use &~ or diff instead of --", "2.13.0")
156-
@`inline` final def -- (that: Set[A @uV]): C = diff(that)
156+
@`inline` final def -- [A1 >: A](that: Set[A1]): C = diff(that)
157157

158158
@deprecated("Consider requiring an immutable Set or fall back to Set.diff", "2.13.0")
159159
def - [A1 >: A](elem: A1): C = diff(Set(elem))

src/library/scala/collection/immutable/HashSet.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ sealed abstract class HashSet[A]
5656
case _ => super.concat(that)
5757
}
5858

59-
override def intersect(that: collection.Set[A]): HashSet[A] = that match {
59+
override def intersect[A1 >: A](that: collection.Set[A1]): HashSet[A] = that match {
6060
case that: HashSet[A] =>
6161
val buffer = new Array[HashSet[A]](bufferSize(this.size min that.size))
6262
nullToEmpty(intersect0(that, 0, buffer, 0))
6363
case _ => super.intersect(that)
6464
}
6565

66-
override def diff(that: collection.Set[A]): HashSet[A] = that match {
66+
override def diff[A1 >: A](that: collection.Set[A1]): HashSet[A] = that match {
6767
case that: HashSet[A] =>
6868
val buffer = new Array[HashSet[A]](bufferSize(this.size))
6969
nullToEmpty(diff0(that, 0, buffer, 0))

0 commit comments

Comments
 (0)