From List[+T]
I understand a list of dogs is also a list of animals which aligns perfectly with the intuition. From def :: [B >: A](elem: B): List[B]
I understand I can add an animal (B
, less specific) to a list of dogs (A
, more specific) and will get back a list of animals. This aligns with the intuition as well. So basically List
is good.
From Array[T]
I understand an array of dogs is not (could not be used in place of a) an array of animals which is rather counterintuitive. An array of dogs is indeed an array of animals as well but obviously Scala disagrees.
I was hoping someone intuitively explain why Array
in invariant, preferably in terms of dogs (or cats).
There is Why are Arrays invariant, but Lists covariant? but I'm looking for a more intuitive explanation that doesn't (heavily) involve the type system.
Related to Why is Scala's immutable Set not covariant in its type?