- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
concurrent map ala java conventions with default
Fri, 2012-01-20, 03:31
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)
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)
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)