Skip to content

Commit d126a91

Browse files
committed
Be more explicit about usage of unsound language features
Subset of scala/collection-strawman#521 containing only changes applied to the collections-contrib module. The other changes have already been ported to scala/scala#6508.
1 parent 8e2bd53 commit d126a91

File tree

3 files changed

+78
-35
lines changed

3 files changed

+78
-35
lines changed

src/main/scala/scala/collection/MultiDict.scala

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package scala.collection
22

3+
import annotation.unchecked.uncheckedVariance
4+
35
/**
46
* A multidict is a map that can associate a set of values to a given key.
57
*
@@ -40,7 +42,7 @@ trait MultiDict[K, V]
4042
trait MultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K, V]]
4143
extends IterableOps[(K, V), Iterable, C] {
4244

43-
protected[this] type MultiDictCC[K, V] = CC[K, V]
45+
protected[this] type MultiDictCC[K, V] = CC[K, V] @uncheckedVariance
4446

4547
def multiMapFactory: MapFactory[MultiDictCC]
4648

@@ -133,13 +135,8 @@ trait MultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K, V]]
133135
def concat(that: Iterable[(K, V)]): C =
134136
fromSpecificIterable(new View.Concat(toIterable, that))
135137

136-
override def withFilter(p: ((K, V)) => Boolean): MultiMapWithFilter = new MultiMapWithFilter(p)
137-
138-
class MultiMapWithFilter(p: ((K, V)) => Boolean) extends WithFilter(p) {
139-
def map[L, W](f: ((K, V)) => (L, W)): CC[L, W] = multiMapFromIterable(new View.Map(filtered, f))
140-
def flatMap[L, W](f: ((K, V)) => IterableOnce[(L, W)]): CC[L, W] = multiMapFromIterable(new View.FlatMap(filtered, f))
141-
override def withFilter(q: ((K, V)) => Boolean): MultiMapWithFilter = new MultiMapWithFilter(kv => p(kv) && q(kv))
142-
}
138+
override def withFilter(p: ((K, V)) => Boolean): MultiDictOps.WithFilter[K, V, IterableCC, CC] =
139+
new MultiDictOps.WithFilter(this, p)
143140

144141
/**
145142
* @return Whether there exists a value associated with the given `key`
@@ -203,4 +200,23 @@ trait MultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K, V]]
203200

204201
}
205202

206-
object MultiDict extends MapFactory.Delegate[MultiDict](immutable.MultiDict)
203+
object MultiDictOps {
204+
205+
class WithFilter[K, V, +IterableCC[_], +CC[X, Y] <: MultiDict[X, Y]](
206+
`this`: MultiDictOps[K, V, CC, _] with IterableOps[(K, V), IterableCC, _],
207+
p: ((K, V)) => Boolean
208+
) extends IterableOps.WithFilter[(K, V), IterableCC](`this`, p) {
209+
210+
def map[L, W](f: ((K, V)) => (L, W)): CC[L, W] =
211+
`this`.multiMapFactory.from(new View.Map(filtered, f))
212+
213+
def flatMap[L, W](f: ((K, V)) => IterableOnce[(L, W)]): CC[L, W] =
214+
`this`.multiMapFactory.from(new View.FlatMap(filtered, f))
215+
216+
override def withFilter(q: ((K, V)) => Boolean): WithFilter[K, V, IterableCC, CC] =
217+
new WithFilter[K, V, IterableCC, CC](`this`, (kv: (K, V)) => p(kv) && q(kv))
218+
}
219+
220+
}
221+
222+
object MultiDict extends MapFactory.Delegate[MultiDict](immutable.MultiDict)

src/main/scala/scala/collection/SortedMultiDict.scala

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package scala.collection
22

3+
import annotation.unchecked.uncheckedVariance
4+
35
/**
46
* A multidict whose keys are sorted
5-
*
67
* @tparam K the type of keys
78
* @tparam V the type of values
89
*/
@@ -20,7 +21,7 @@ trait SortedMultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K,
2021
extends MultiDictOps[K, V, MultiDict, C]
2122
with SortedOps[K, C] {
2223

23-
protected[this] type SortedMultiDictCC[K, V] = CC[K, V]
24+
protected[this] type SortedMultiDictCC[K, V] = CC[K, V] @uncheckedVariance
2425

2526
def sortedMultiMapFactory: SortedMapFactory[SortedMultiDictCC]
2627

@@ -51,13 +52,8 @@ trait SortedMultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K,
5152
until(next)
5253
}
5354

54-
override def withFilter(p: ((K, V)) => Boolean): SortedMultiMapWithFilter = new SortedMultiMapWithFilter(p)
55-
56-
class SortedMultiMapWithFilter(p: ((K, V)) => Boolean) extends MultiMapWithFilter(p) {
57-
def map[L : Ordering, W](f: ((K, V)) => (L, W)): CC[L, W] = sortedFromIterable(new View.Map(filtered, f))
58-
def flatMap[L : Ordering, W](f: ((K, V)) => IterableOnce[(L, W)]): CC[L, W] = sortedFromIterable(new View.FlatMap(filtered, f))
59-
override def withFilter(q: ((K, V)) => Boolean): SortedMultiMapWithFilter = new SortedMultiMapWithFilter(kv => p(kv) && q(kv))
60-
}
55+
override def withFilter(p: ((K, V)) => Boolean): SortedMultiDictOps.WithFilter[K, V, IterableCC, MultiDictCC, CC] =
56+
new SortedMultiDictOps.WithFilter[K, V, IterableCC, MultiDictCC, CC](this, p)
6157

6258
/**
6359
* @return a sorted multidict that contains all the entries of `this` sorted multidict,
@@ -130,4 +126,24 @@ trait SortedMultiDictOps[K, V, +CC[X, Y] <: MultiDict[X, Y], +C <: MultiDict[K,
130126

131127
}
132128

133-
object SortedMultiDict extends SortedMapFactory.Delegate[SortedMultiDict](immutable.SortedMultiDict)
129+
object SortedMultiDictOps {
130+
131+
class WithFilter[K, V, +IterableCC[_], +MultiDictCC[X, Y] <: MultiDict[X, Y], +CC[X, Y] <: MultiDict[X, Y]](
132+
`this`: SortedMultiDictOps[K, V, CC, _] with MultiDictOps[K, V, MultiDictCC, _] with IterableOps[(K, V), IterableCC, _],
133+
p: ((K, V)) => Boolean
134+
) extends MultiDictOps.WithFilter[K, V, IterableCC, MultiDictCC](`this`, p) {
135+
136+
def map[L : Ordering, W](f: ((K, V)) => (L, W)): CC[L, W] =
137+
`this`.sortedMultiMapFactory.from(new View.Map(filtered, f))
138+
139+
def flatMap[L : Ordering, W](f: ((K, V)) => IterableOnce[(L, W)]): CC[L, W] =
140+
`this`.sortedMultiMapFactory.from(new View.FlatMap(filtered, f))
141+
142+
override def withFilter(q: ((K, V)) => Boolean): WithFilter[K, V, IterableCC, MultiDictCC, CC] =
143+
new WithFilter[K, V, IterableCC, MultiDictCC, CC](`this`, kv => p(kv) && q(kv))
144+
145+
}
146+
147+
}
148+
149+
object SortedMultiDict extends SortedMapFactory.Delegate[SortedMultiDict](immutable.SortedMultiDict)

src/main/scala/scala/collection/SortedMultiSet.scala

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ trait SortedMultiSetOps[A, +CC[X] <: MultiSet[X], +C <: MultiSet[A]]
2424
extends MultiSetOps[A, MultiSet, C]
2525
with SortedOps[A, C] {
2626

27-
protected[this] type SortedIterableCC[X] = CC[X]
27+
protected[this] type SortedIterableCC[X] = CC[X] @uncheckedVariance
2828

2929
def sortedIterableFactory: SortedIterableFactory[SortedIterableCC]
3030

@@ -62,21 +62,8 @@ trait SortedMultiSetOps[A, +CC[X] <: MultiSet[X], +C <: MultiSet[A]]
6262
until(next)
6363
}
6464

65-
override def withFilter(p: A => Boolean): SortedWithFilter = new SortedWithFilter(p)
66-
67-
/** Specialize `WithFilter` for sorted collections
68-
*
69-
* @define coll sorted collection
70-
*/
71-
class SortedWithFilter(p: A => Boolean) extends WithFilter(p) {
72-
73-
def map[B : Ordering](f: A => B): CC[B] = sortedIterableFactory.from(new View.Map(filtered, f))
74-
75-
def flatMap[B : Ordering](f: A => IterableOnce[B]): CC[B] = sortedIterableFactory.from(new View.FlatMap(filtered, f))
76-
77-
override def withFilter(q: A => Boolean): SortedWithFilter = new SortedWithFilter(a => p(a) && q(a))
78-
79-
}
65+
override def withFilter(p: A => Boolean): SortedMultiSetOps.WithFilter[A, IterableCC, CC] =
66+
new SortedMultiSetOps.WithFilter(this, p)
8067

8168
/** Builds a new sorted multiset by applying a function to all elements of this sorted multiset.
8269
*
@@ -169,4 +156,28 @@ trait SortedMultiSetOps[A, +CC[X] <: MultiSet[X], +C <: MultiSet[A]]
169156

170157
}
171158

159+
object SortedMultiSetOps {
160+
161+
/** Specialize `WithFilter` for sorted collections
162+
*
163+
* @define coll sorted collection
164+
*/
165+
class WithFilter[A, +IterableCC[_], +CC[X] <: MultiSet[X]](
166+
`this`: SortedMultiSetOps[A, CC, _] with IterableOps[A, IterableCC, _],
167+
p: A => Boolean
168+
) extends IterableOps.WithFilter(`this`, p) {
169+
170+
def map[B : Ordering](f: A => B): CC[B] =
171+
`this`.sortedIterableFactory.from(new View.Map(filtered, f))
172+
173+
def flatMap[B : Ordering](f: A => IterableOnce[B]): CC[B] =
174+
`this`.sortedIterableFactory.from(new View.FlatMap(filtered, f))
175+
176+
override def withFilter(q: A => Boolean): WithFilter[A, IterableCC, CC] =
177+
new WithFilter[A, IterableCC, CC](`this`, a => p(a) && q(a))
178+
179+
}
180+
181+
}
182+
172183
object SortedMultiSet extends SortedIterableFactory.Delegate(immutable.SortedMultiSet)

0 commit comments

Comments
 (0)