Covariance, contravariance and invariance describe how the existing type inheritance hierarchy changes when subjected to some transformation (such as usage within generics). If the transformation keeps the ordering of the original hierarchy, it is "covariant". If it reverses it, it is "contravariant". If it breaks it, it is "invariant".
Questions tagged [invariance]
49 questions
27
votes
1 answer
Difference between Variance, Covariance, Contravariance and Bivariance in TypeScript
Could you please explain using small and simple TypeScript examples what is Variance, Covariance, Contravariance and Bivariance?

captain-yossarian from Ukraine
- 31,174
- 3
- 30
- 62
24
votes
2 answers
What are good reasons for choosing invariance in an API like Stream.reduce()?
Reviewing Java 8 Stream API design, I was surprised by the generic invariance on the Stream.reduce() arguments:
U reduce(U identity,
BiFunction accumulator,
BinaryOperator combiner)
A seemingly more…

Lukas Eder
- 211,314
- 129
- 689
- 1,509
20
votes
2 answers
OCaml variance (+'a, -'a) and invariance
After writing this piece of code
module type TS = sig
type +'a t
end
module T : TS = struct
type 'a t = {info : 'a list}
end
I realised I needed info to be mutable.
I wrote, then :
module type TS = sig
type +'a t
end
module T : TS =…

Lhooq
- 4,281
- 1
- 18
- 37
9
votes
1 answer
Vector{AbstractString} function parameter won't accept Vector{String} input in julia
The following code in Julia:
function foo(a::Vector{AbstractString})
end
foo(["a"])
gives the following error:
ERROR: MethodError: no method matching foo(::Array{String,1})
Closest candidates are:
foo(::Array{AbstractString,1}) at…

TheGlamburglar
- 91
- 1
7
votes
2 answers
How do I deal with wrapper type invariance in Rust?
References to wrapper types like &Rc and &Box are invariant in T (&Rc is not a &Rc even if T is a U). A concrete example of the issue (Rust Playground):
use std::rc::Rc;
use std::rc::Weak;
trait MyTrait {}
struct MyStruct {
}
impl…

Erik Vesteraas
- 4,675
- 2
- 24
- 37
6
votes
3 answers
Invariant Generics don't seem working correctly
I've read some articles about Covariance, Contravariance, and Invariance in Java, but I'm confused about them.
I'm using Java 11, and I have a class hierarchy A => B => C (means that C is a subtype of B and A, and B is a subtype of A) and a class…

willdel
- 63
- 5
5
votes
4 answers
Unable to hold generic interface implementations in list c#
How to make a list hold all the different implementations of generic interface?
e.g
public class Animal { // some Animal implementations }
public class Dog : Animal { // some Dog implementations }
public class Snake : Animal { // some Snake…

kartik rajan
- 115
- 1
- 5
5
votes
2 answers
Why are all invariant generic class positions invariant in type parameter lists in Scala?
I'm a bit perplexed by the strictness of the typechecker below — it seems that the invariant T position of Inv[T] is also invariant within Variantish's parameter list:
scala> class Inv[T]
defined class Inv
scala> class Variantish[+T, +TVar <:…

concat
- 3,107
- 16
- 30
4
votes
1 answer
Is there a non-covariant `Type[T]`?
Suppose I'm trying to write type hints for a library function that registers a deserializer for a user-defined type: the user should provide a type T along with a function decode: str -> T.
The most natural way I can think to write this with…

Anthony Carapetis
- 560
- 3
- 10
4
votes
1 answer
Intuitively explain why `List` is covariant but `Array` is invariant?
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…

Ashkan Kh. Nazary
- 21,844
- 13
- 44
- 68
4
votes
1 answer
Getting around invariant result type in State
I would like to define a State that builds a concrete subtype of a trait, as per decodeFoo:
sealed trait Foo
case class Bar(s: String) extends Foo
case class Baz(i: Int) extends Foo
val int: State[Seq[Byte], Int] = State[Seq[Byte], Int] {
case bs…

Synesso
- 37,610
- 35
- 136
- 207
4
votes
1 answer
I've read all about Covariance, Contravariance and Invariance, but I still don't get how to design my code
I've searched and read/studied as much as seemed reasonable prior to posting this. I have found similar questions, but most posts actually relate more to passing "List of a derived types" to functions calls that require "List of a base type". I can…

Doug Conyers
- 55
- 5
4
votes
0 answers
The meaning of "capture of ? e" in Java
In IntelliJ IDEA, especially when I create the instance of Collection class like:
Collection> collection = ...;
collection.
And try typing the method add, IntelliJ helps me telling that add's first argument is capture of ? e.
Anyway, I can't use…

Beraliv
- 518
- 7
- 20
3
votes
4 answers
Only allow exact type (not subclasses) in C++
Is is possible to declare types where it only allows that class and not any subclasses (I know this violates the Liskov substitution principle but I still want to know if there's a way to do it.)
For example,
#include
struct A {};
struct…

MarcellPerger
- 405
- 3
- 13
3
votes
1 answer
Array: get and set Int values without casting
I am building a Matrix class and want to be able to store Numbers in a 2d Array.
var data: Array> = Array(width, {Array(height, {0})})
This does not work because Array and Array are invariant.
I can make it work by using…

Patric
- 1,489
- 13
- 28