Why aren't HashMaps semi-groups, but Maps?

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 aren't HashMaps semi-groups and Maps not? Based on an object-oriented background, I would expect that a HashMap is capable of every bit as a map?

+4
source share
1 answer

Because Semigroup is invariant and there is no instance defined for HashMap , just Map . Invariance basically means that the syntax |+| cannot use a Semigroup instance for Map for a type that displays as HashMap , although HashMap is a subtype of Map .

In some cases, Scalaz scales are uselessly invariant. However, this is not one of these cases. Because of the function signature |+| variance (either co-or contra) does not make much sense, and therefore the vertex class is in its maximum generality.

+4
source

Source: https://habr.com/ru/post/1499443/


All Articles