scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Most s.c.c.Map methods return s.c.m.Map-typed values #12117

Open NthPortal opened 4 years ago

NthPortal commented 4 years ago

concurrent.Map[K, V] does not extend mutable.MapOps[K, V, concurrent.Map, concurrent.Map[K, V]], and thus its methods return mutable.Map or types based on the same. For example:

yzia2000 commented 1 year ago

Interesting. Was this done intentionally?

SethTisue commented 1 year ago

@scala/collections

He-Pin commented 1 year ago

I tested it with image

But yes, it should , otherwise when programming with it, the type info is lost, have not checkout the scala/scala source code to check it out.

jxnu-liguobin commented 1 year ago

I tested it with image

But yes, it should , otherwise when programming with it, the type info is lost, have not checkout the scala/scala source code to check it out.

It looks like scala3 has been fixed

som-snytt commented 1 year ago

Let's avoid posting pictures. Code in triple backquoted blocks for formatting. Then we can cut/paste your snippets. Thanks!

The OP claim was that it doesn't extend mutable.MapOps:

final class TrieMap[K, V] extends mutable.AbstractMap[K, V] with Map[K, V] with mutable.MapOps[K, V, TrieMap, TrieMap[K, V]] with MapFactoryDefaults[K, V, TrieMap, mutable.Iterable] with DefaultSerializable

Surprisingly difficult to cut/paste from Scaladoc. I had to paste to a terminal to get it without links.

Anyway, the OP claim is not about TrieMap.

scala> import collection.concurrent.TrieMap
import collection.concurrent.TrieMap

scala> val m = TrieMap("one"->1, "fortytwo"->42)
val m: scala.collection.concurrent.TrieMap[String,Int] = TrieMap(fortytwo -> 42, one -> 1)

scala> import collection.concurrent.{Map => CMap}
import collection.concurrent.{Map=>CMap}

scala> val cm: CMap[String, Int] = m
val cm: scala.collection.concurrent.Map[String,Int] = TrieMap(fortytwo -> 42, one -> 1)

scala> cm.filter { case (s, i) => s.startsWith("f") }
val res0: scala.collection.mutable.Map[String,Int] = TrieMap(fortytwo -> 42)

And so

scala> type Thing[K, V] = scala.collection.concurrent.Map[K, V] with scala.collection.mutable.MapOps[K, V, TrieMap, Trie
Map[K, V]]
type Thing

scala> val cm: Thing[String, Int] = m
val cm: Thing[String,Int] = TrieMap(fortytwo -> 42, one -> 1)

scala> cm.filter { case (s, i) => s.startsWith("f") }
val res2: scala.collection.concurrent.TrieMap[String,Int] = TrieMap(fortytwo -> 42)

scala> type Thing[K, V] = scala.collection.concurrent.Map[K, V] with scala.collection.mutable.MapOps[K, V, CMap, CMap[K,
 V]]
                          ^
       error: type arguments [K,V,scala.collection.concurrent.Map,scala.collection.concurrent.Map[K,V]] do not conform to trait MapOps's type parameter bounds [K,V,+CC[X, Y] <: scala.collection.mutable.MapOps[X, Y, CC, _],+C <: scala.collection.mutable.MapOps[K,V,CC,C]]