def foo(a: Map[String, Int], b: HashMap[String, Int]) {
// okay
val ab = a |+| b
// value |+| is not a member of scala.collection.immutable.HashMap[String,Int]
val ba = b |+| a
}
Why are HashMaps not Semigroups, but Maps are? Coming from an object-oriented background, I would have expected that a HashMap is every bit as capable as a Map?
Because
Semigroup
is invariant and there is no instance defined for specificallyHashMap
, justMap
. The invariance basically means that the|+|
syntax cannot use theSemigroup
instance forMap
on a type which is inferred to beHashMap
, even thoughHashMap
is a subtype ofMap
.In some cases, Scalaz typeclasses are needlessly invariant. This is not one of those cases though. Because of the signature of the
|+|
function, variance (either co- or contra-) wouldn't make much sense, and thus the typeclass is correct in its maximal generality.