This page is no longer maintained — Please continue to the home page at www.scala-lang.org

concurrent map ala java conventions with default

1 reply
Doug Tangren
Joined: 2009-12-10,
User offline. Last seen 42 years 45 weeks ago.
Is the following expected behavior for a concurrent map and a declared default method?
http://www.scala-lang.org/api/current/scala/collection/mutable/ConcurrentMap.html hints that I may define a `default` method to handle cases where keys are not found. Given that I am really waving the java conventions magic wand below, should I expect the scala doc for `default` to be honored or no?


scala> import java.util.concurrent._ import java.util.concurrent._
scala>   import scala.collection.JavaConversions._import scala.collection.JavaConversions._
scala> val m: ConcurrentMap[String, Int] =      |   new ConcurrentHashMap[String, Int] {     |     def default(k: String) = 42     |   }m: java.util.concurrent.ConcurrentMap[String,Int] = {}
scala> m("default?") java.util.NoSuchElementException: key not found: default? at scala.collection.MapLike$class.default(MapLike.scala:224) at scala.collection.JavaConversions$JConcurrentMapWrapper.default(JavaConversions.scala:820) at scala.collection.MapLike$class.apply(MapLike.scala:135)
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: concurrent map ala java conventions with default

the design is a bit odd... in any case you are adding a method to the java class (instead of overriding a method in the scala class) so that can't work.

i would think that this should work:

val m: ConcurrentMap[String, Int] =
new JConcurrentMapWrapper( new ConcurrentHashMap[String, Int]) {
override def default(k: String) = 42
}

m("default?")

but it still doesn't, probably because with the cast to the scala class you insert a new jwrapper around it which doesn't honor default.

this works though

val m = new JConcurrentMapWrapper( new ConcurrentHashMap[String, Int]) {
override def default(k: String) = 42
}

m("default?")

On 20 Jan 2012, at 02:30, Doug Tangren wrote:

> Is the following expected behavior for a concurrent map and a declared default method?
>
> http://www.scala-lang.org/api/current/scala/collection/mutable/Concurren... hints that I may define a `default` method to handle
> cases where keys are not found. Given that I am really waving the java conventions magic wand below, should I expect the scala doc for `default` to be honored or no?
>
>
> scala> import java.util.concurrent._
> import java.util.concurrent._
>
> scala> import scala.collection.JavaConversions._
> import scala.collection.JavaConversions._
>
> scala> val m: ConcurrentMap[String, Int] =
> | new ConcurrentHashMap[String, Int] {
> | def default(k: String) = 42
> | }
> m: java.util.concurrent.ConcurrentMap[String,Int] = {}
>
> scala> m("default?")
> java.util.NoSuchElementException: key not found: default?
> at scala.collection.MapLike$class.default(MapLike.scala:224)
> at scala.collection.JavaConversions$JConcurrentMapWrapper.default(JavaConversions.scala:820)
> at scala.collection.MapLike$class.apply(MapLike.scala:135)

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland