From 979c57cd8732a2f9c9de066d532971986bcdff7d Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Fri, 3 Dec 2010 06:17:08 +0000 Subject: immutable.Map keySet returns immutable.Set. --- src/library/scala/collection/immutable/MapLike.scala | 7 +++++-- src/library/scala/collection/immutable/MapProxy.scala | 1 + src/library/scala/collection/immutable/SortedMap.scala | 2 +- test/files/neg/type-diagnostics.check | 4 ++-- test/files/neg/type-diagnostics.scala | 2 +- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/library/scala/collection/immutable/MapLike.scala b/src/library/scala/collection/immutable/MapLike.scala index bbad195687..0e2723cf2b 100644 --- a/src/library/scala/collection/immutable/MapLike.scala +++ b/src/library/scala/collection/immutable/MapLike.scala @@ -50,8 +50,6 @@ trait MapLike[A, +B, +This <: MapLike[A, B, This] with Map[A, B]] extends scala.collection.MapLike[A, B, This] { self => - import scala.collection.Traversable - /** A new immutable map containing updating this map with a given key/value mapping. * @param key the key * @param value the value @@ -110,6 +108,11 @@ trait MapLike[A, +B, +This <: MapLike[A, B, This] with Map[A, B]] def get(key: A) = self.get(key).map(f) } + /** Collects all keys of this map in a set. + * @return a set containing all keys of this map. + */ + override def keySet: immutable.Set[A] = immutable.Set.empty ++ (this map (_._1)) + /** This function transforms all the values of mappings contained * in this map with function `f`. * diff --git a/src/library/scala/collection/immutable/MapProxy.scala b/src/library/scala/collection/immutable/MapProxy.scala index fa0b097070..8313e4d143 100644 --- a/src/library/scala/collection/immutable/MapProxy.scala +++ b/src/library/scala/collection/immutable/MapProxy.scala @@ -35,6 +35,7 @@ trait MapProxy[A, +B] extends Map[A, B] with MapProxyLike[A, B, Map[A, B]] { override def + [B1 >: B](elem1: (A, B1), elem2: (A, B1), elems: (A, B1) *) = newProxy(self.+(elem1, elem2, elems: _*)) override def ++[B1 >: B](xs: TraversableOnce[(A, B1)]) = newProxy(self ++ xs) + override def keySet: immutable.Set[A] = new SetProxy[A] { val self = MapProxy.this.self.keySet } override def filterKeys(p: A => Boolean) = self.filterKeys(p) override def mapValues[C](f: B => C) = self.mapValues(f) } diff --git a/src/library/scala/collection/immutable/SortedMap.scala b/src/library/scala/collection/immutable/SortedMap.scala index f49c4a9986..aa30932bb7 100644 --- a/src/library/scala/collection/immutable/SortedMap.scala +++ b/src/library/scala/collection/immutable/SortedMap.scala @@ -36,8 +36,8 @@ trait SortedMap[A, +B] extends Map[A, B] SortedMap.newBuilder[A, B] override def empty: SortedMap[A, B] = SortedMap.empty - override def updated [B1 >: B](key: A, value: B1): SortedMap[A, B1] = this + ((key, value)) + override def keySet: immutable.SortedSet[A] = SortedSet.empty ++ (this map (_._1)) /** Add a key/value pair to this map. * @param kv the key/value pair diff --git a/test/files/neg/type-diagnostics.check b/test/files/neg/type-diagnostics.check index 0ed9f4631b..c5e6dec3f8 100644 --- a/test/files/neg/type-diagnostics.check +++ b/test/files/neg/type-diagnostics.check @@ -1,8 +1,8 @@ type-diagnostics.scala:4: error: type mismatch; found : scala.collection.Set[String] required: scala.collection.immutable.Set[String] - def f = Calculator("Hello",binding.keySet) - ^ + def f = Calculator("Hello", binding.keySet: collection.Set[String]) + ^ type-diagnostics.scala:13: error: type mismatch; found : List[a(in method f2)] required: List[a(in method f1)] diff --git a/test/files/neg/type-diagnostics.scala b/test/files/neg/type-diagnostics.scala index fcc49812f4..c4171328de 100644 --- a/test/files/neg/type-diagnostics.scala +++ b/test/files/neg/type-diagnostics.scala @@ -1,7 +1,7 @@ object SetVsSet { case class Calculator[+T](name: String, parameters: Set[String]) val binding = Map.empty[String, String] - def f = Calculator("Hello",binding.keySet) + def f = Calculator("Hello", binding.keySet: collection.Set[String]) } object TParamConfusion { -- cgit v1.2.3