For some weird reason, there is this one method merged
that is only declared in scala.collection.immutable.HashMap
but not in generic Map
trait (perhaps its implementation makes it very unpalatable to other Map
implementations?).
So I need to convert my Map[A, B]
to HashMap[A, B]
and so far I cannot find an easy way to do so. This is my current implementation to hack around def to[Col[_]]
which expects unary higher-kind instead of binary higher-kind.
val m1 = Map("foo" -> 1)
val m2 = Map("foo" -> 2, "bar" -> 2)
type HM[_] = HashMap[String, Int]
(m1.to[HM] merged m2.to[HM]) { case ((k1, v1), (k2, v2)) => (k1, v1 + v2) } // Map("foo" -> 3, "bar" -> 2)
It works as expected but I can't help but to think there must be a better way to convert from Map
to HashMap
(given it's the default implementation).
Or perhaps in more general, better way to access default implementation of scala collection generic traits?