diff options
author | Paolo Giarrusso <p.giarrusso@gmail.com> | 2012-08-22 20:22:54 +0200 |
---|---|---|
committer | Paolo Giarrusso <p.giarrusso@gmail.com> | 2012-08-22 20:22:54 +0200 |
commit | bc621bad33cc897f43f2b30d1c8afe1912919cfe (patch) | |
tree | 589ace94e21eef48a60bd3d4f01a36dadc3ecf08 /test/files/pos | |
parent | 079e9e33327c497572c918f7d3155882b484e9ff (diff) | |
download | scala-bc621bad33cc897f43f2b30d1c8afe1912919cfe.tar.gz scala-bc621bad33cc897f43f2b30d1c8afe1912919cfe.tar.bz2 scala-bc621bad33cc897f43f2b30d1c8afe1912919cfe.zip |
Ensure implicit conversions to concurrent map are unambiguous
Even after the parent change, implicit conversions should still convert
ConcurrentHashMap to concurrent.Map and not to mutable.ConcurrentMap.
I'll include the comment by axel22 which prompting this change (taken
from https://github.com/scala/scala/pull/1172#issuecomment-7928285)
since it is highly informative and links to pull request comments might
not be very stable:
<<
Instead of just testing an implicit conversion to ConcurrentMap, the
test should call ConcurrentMap methods such as putIfAbsent (which
returns an Option[T]) and replace, to see if the correct Scala
concurrent map trait is being resolved.
The problem is that putIfAbsent already exists on juc.ConcurrentMap so
instead of triggering an implicit conversion, a type mismatch error is
output anyway.
And we cannot test that the correct concurrent map trait is returned
using methods like map, flatMap and friends, because concurrent map
traits extends mutable.Map anyway.
For this reason, I recommend to add this to the test:
scala> val a = new java.util.concurrent.ConcurrentHashMap[String,
String]() += (("", ""))
a: scala.collection.concurrent.Map[String,String] = Map("" -> "")
and make sure that the returned map is concurrent.Map, not
mutable.ConcurrentMap (the above += is one of the few methods in
collection that has the return type this.type).
>>
Diffstat (limited to 'test/files/pos')
-rw-r--r-- | test/files/pos/javaConversions-2.10-regression.scala | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/test/files/pos/javaConversions-2.10-regression.scala b/test/files/pos/javaConversions-2.10-regression.scala index 619c44b46d..e1b81015ba 100644 --- a/test/files/pos/javaConversions-2.10-regression.scala +++ b/test/files/pos/javaConversions-2.10-regression.scala @@ -1,11 +1,17 @@ -import collection.{JavaConversions, mutable} +import collection.{JavaConversions, mutable, concurrent} import JavaConversions._ -import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.{ConcurrentHashMap => CHM} object Foo { def buildCache2_9_simple[K <: AnyRef, V <: AnyRef]: mutable.ConcurrentMap[K, V] = - asScalaConcurrentMap(new ConcurrentHashMap()) + asScalaConcurrentMap(new CHM()) def buildCache2_9_implicit[K <: AnyRef, V <: AnyRef]: mutable.ConcurrentMap[K, V] = - new ConcurrentHashMap[K, V]() + new CHM[K, V]() +} + +object Bar { + def assertType[T](t: T) = t + val a = new CHM[String, String]() += (("", "")) + assertType[concurrent.Map[String, String]](a) } |