|
| 1 | +package strawman |
| 2 | +package collection.immutable |
| 3 | + |
| 4 | +import strawman.collection.{IterablePolyTransforms, MapPolyTransforms, Sorted, SortedLike} |
| 5 | + |
| 6 | +import scala.annotation.unchecked.uncheckedVariance |
| 7 | +import scala.Ordering |
| 8 | + |
| 9 | +trait SortedMap[K, +V] |
| 10 | + extends Map[K, V] |
| 11 | + with Sorted[K] |
| 12 | + with SortedMapLike[K, V, SortedMap] |
| 13 | + |
| 14 | +trait SortedMapLike[K, +V, +C[X, +Y] <: SortedMap[X, Y]] |
| 15 | + extends SortedLike[K, C[K, V]] |
| 16 | + with SortedMapPolyTransforms[K, V, C] |
| 17 | + with MapLike[K, V, Map] // Inherited Map operations can only return a `Map` because they don’t take an evidence `Ordering` |
| 18 | + with MapMonoTransforms[K, V, C[K, V]] // Operations that return the same collection type can return a `SortedMap`, though |
| 19 | + |
| 20 | +/** Polymorphic transformation methods for sorted Maps */ |
| 21 | +trait SortedMapPolyTransforms[K, +V, +C[X, Y] <: Sorted[X]] |
| 22 | + // We inherit polymorphic transformations returning an Iterable (e.g. to |
| 23 | + // support the following use case `kvs.map((k, v) => v)`) |
| 24 | + extends IterablePolyTransforms[(K, V), Iterable] |
| 25 | + // Then we also inherit polymorphic transformations returning a Map, just |
| 26 | + // to get inheritance linearization right and disambiguate between |
| 27 | + // overloaded methods |
| 28 | + with MapPolyTransforms[K, V, Map] { |
| 29 | + |
| 30 | + // And finally, we add new overloads taking an ordering |
| 31 | + def map[K2, V2](f: (K, V) => (K2, V2))(implicit ordering: Ordering[K2]): C[K2, V2] |
| 32 | + |
| 33 | + /** |
| 34 | + * Add a key/value pair to this map, returning a new map. |
| 35 | + * |
| 36 | + * @param kv the key/value pair. |
| 37 | + * @tparam V1 the type of the value in the key/value pair. |
| 38 | + * @return A new map with the new binding added to this map. |
| 39 | + */ |
| 40 | + def + [V1 >: V](kv: (K, V1)): C[K, V1] |
| 41 | + |
| 42 | +} |
| 43 | + |
0 commit comments