Since, as Garrett Rowe noted, withDefaultValue
doesn't preserve the proper MultiMap
type when using the mixin, you can instead override the default
method in an anonymous class and preserve the behavior of MultiMap
:
scala> import collection.mutable.{ HashMap, MultiMap, Set }
import collection.mutable.{HashMap, MultiMap, Set}
scala> val map: MultiMap[String, Int] = new HashMap[String, Set[Int]] with MultiMap[String, Int] {
| override def default(key: String): Set[Int] = Set.empty[Int]
| }
map: scala.collection.mutable.MultiMap[String,Int] = Map()
scala> map("foo")
res0: scala.collection.mutable.Set[Int] = Set()
scala> map.addBinding("foo", 1)
res1: map.type = Map(foo -> Set(1))
scala> map("foo")
res2: scala.collection.mutable.Set[Int] = Set(1)