I want to extends a class with a val of mutable.HashMap[] like this:
class Father
class Son extends Father
class C1{
val m = new mutable.HashMap[Int, Father]()
}
class C2 extends C1{
override val m = new mutable.HashMap[Int, Son]()
}
And get an error:
Error:(19, 16) overriding value m in class C1 of type scala.collection.mutable.HashMap[Int,ScalaByExample.Father]; value m has incompatible type override val m = new mutable.HashMapInt, Son
I found that immutable.HashMap
is covariant but mutable.HashMap
is invariant. It works if replace mutable.HashMap
with immutable.HashMap
.
So my two questions are:
How can I make it works using mutable.HashMap?
Why did scala's author design HashMap like this?