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
Semigroupis invariant and there is no instance defined for specificallyHashMap, justMap. The invariance basically means that the|+|syntax cannot use theSemigroupinstance forMapon a type which is inferred to beHashMap, even thoughHashMapis 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.