3 ms·
That's a gratuitously category-theoretic explanation, all you need to explain covariance and contravariance is is-a. An immutable List of Foos is a List of Bar
by bcoates 13y ago
That's a gratuitously category-theoretic explanation, all you need to explain covariance and contravariance is is-a.
An immutable List of Foos is a List of Bars if a Foo is a Bar. A function taking X and returning Y is a function taking A and returning B if A is a X and Y is a B.
It's just answering the question "Can I use type relationships to guarantee that I will have no run-time conversion failures?"
- deleted 13y ago[deleted]
- evincarofautumn 13y agoT is covariant (positive) if A ≤: B ⇒ T<A> ≤: T<B>. T is contravariant (negative) if A ≤: B ⇒ T<B> ≤: T<A> (i.e., the other way around). Pretty simple stuff.
- pkolaczk 13y agoIt is easy to remember if you think of it: + (covariant): the type can be cast to some broader (more general) type - (contravariant): the type can be cast to some narrower (more concrete) type