Difference between Variance, Covariance, Contravariance, Bivariance and Invariance in TypeScript

Variance has to do with how a generic type F<T> varies with respect to its type parameter T. If you know that T extends U, then variance will tell you whether you can conclude that F<T> extends F<U>, conclude that F<U> extends F<T>, or neither, or both. Covariance means that F<T> and T co–vary. That … Read more

What is the difference between covariance and contra-variance in programming languages? [closed]

Covariance is pretty simple and best thought of from the perspective of some collection class List. We can parameterize the List class with some type parameter T. That is, our list contains elements of type T for some T. List would be covariant if S is a subtype of T iff List[S] is a subtype … Read more

Why is Function[-A1,…,+B] not about allowing any supertypes as parameters?

Covariance and contravariance are qualities of the class not qualities of the parameters. (They are qualities that depend on the parameters, but they make statements about the class.) So, Function1[-A,+B] means that a function that takes superclasses of A can be viewed as a subclass of the original function. Let’s see this in practice: class … Read more

C# variance annotation of a type parameter, constrained to be value type

Why this is allowed by compiler since variance annotation make completely no sense in a such situation? It’s allowed by the compiler because I never even considered that someone might try to do that when I added the variance rules to the C# 4.0 compiler. Compiler warnings and errors are features, and in order for … Read more

Contravariance explained

Update: Ooops. As it turned out, I mixed up variance and “assignment compatibility” in my initial answer. Edited the answer accordingly. Also I wrote a blog post that I hope should answer such questions better: Covariance and Contravariance FAQ Answer: I guess the answer to your first question is that you don’t have contravariance in … Read more

Simple examples of co and contravariance

Could someone provide me simple C# examples of convariance, contravariance, invariance and contra-invariance (if such thing exists). I have no idea what “contra-invariance” means. The rest are easy. Here’s an example of covariance: void FeedTheAnimals(IEnumerable<Animal> animals) { foreach(Animal animal in animals) animal.Feed(); } … List<Giraffe> giraffes = …; FeedTheAnimals(giraffes); The IEnumerable<T> interface is covariant. The … Read more

No warning or error (or runtime failure) when contravariance leads to ambiguity

I believe the compiler does the better thing in VB.NET with the warning, but I still don’t think that is going far enough. Unfortunately, the “right thing” probably either requires disallowing something that is potentially useful(implementing the same interface with two covariant or contravariant generic type parameters) or introducing something new to the language. As … Read more

C# : Is Variance (Covariance / Contravariance) another word for Polymorphism?

It’s certainly related to polymorphism. I wouldn’t say they’re just “another word” for polymorphism though – they’re about very specific situations, where you can treat one type as if it were another type in a certain context. For instance, with normal polymorphism you can treat any reference to a Banana as a reference to a … Read more