How does Java’s use-site variance compare to C#’s declaration site variance?

I am just going to answer the differences between declaration-site and use-site variance, since, while C# and Java generics differ in many other ways, those differences are mostly orthogonal to variance. First off, if I remember correctly use-site variance is strictly more powerful than declaration-site variance (although at the cost of concision), or at least … Read more

When is @uncheckedVariance needed in Scala, and why is it used in GenericTraversableTemplate?

The problem is that GenericTraversableTemplate is used twice: once for mutable collections (where its type parameter should be invariant), and once for immutable collections (where covariance is invariably king). GenericTraversableTemplate’s typechecks assuming either covariance or invariance for the A type parameter. However, when we inherit it in a mutable trait, we have to pick invariance. … 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

Difference between Variance, Covariance, Contravariance and Bivariance 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

Difference between Variance, Covaraince, Contravariance and Bivariance 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