1 year ago
#351272
Quelklef
Does mutation break contravariance in the same way it breaks covariance?
Mutative covariant containers are unsound.
For example, and using no language in particular,
interface Pet;
class Cat extends Pet { meow(); }
class Dog extends Pet { woof(); }
class Box[T] {
value: T;
}
var dog: Box[Dog] = new Box(new Dog);
var pet: Box[Pet] = dog;
pet.value = new Cat;
dog.woof(); // uh oh!
Is there an analogous unsoundness with contravariant types?
My intuition is no. Loosely, the above unsoundness comes from the fact that a Box[T]
"contains" a reference to a T
, but a contravariant type like Predicate[T]
does not "contain" anything at all.
However, I can't find much on the subjet.
covariance
contravariance
type-systems
0 Answers
Your Answer