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
3
votes
1 answer
mypy generic subclass leads to incompatible types
I'm toying around with implementing monoids with type hinting. To that end I've written:
M = TypeVar('M')
class Monoid(Generic[M]):
...
def append(self, m: 'Monoid[M]') -> 'Monoid[M]':
raise NotImplementedError()
When using this in…

Sune Andreas Dybro Debel
- 481
- 2
- 4
- 13
3
votes
1 answer
Generics are invariant, but this compiles without error
I was getting type mismatch errors, until I refactored code to this:
public final Stream> orderedStreamOfEntries() {
return this.m_map.entrySet()
.stream()
…

scottb
- 9,908
- 3
- 40
- 56
3
votes
3 answers
Why is it possible to cast generic class?
Java generics are invariant so it's impossible to do such cast:
List

ctomek
- 1,696
- 16
- 35
2
votes
3 answers
Why does Kotlin produce an Unchecked Cast warning when casting a covariant type parameter to invariant type parameter?
Suppose I have a class Message and a class Channel.
Now, how come I can't cast Channel to Channel without an Unchecked Cast warning?
Shouldn't this cast always be safe since Channel

Archmede
- 1,592
- 2
- 20
- 37
2
votes
1 answer
puzzled by "Already defines a member with the same parameter types" error
Not understanding why these to Map methods have the same parameter types, since they don't appear too. Is this a covariance thing or just a generic signature thing?
I would like to understand it in general to avoid writing code that will have this…

Jon B
- 352
- 2
- 9
2
votes
5 answers
Is it possible to have an interface for Java arrays?
Let's say we want to have a method in an interface that returns an array, like this:
interface A {
B[] findAllB();
}
But arrays are very low-level and implemented definitively. Their implementation is final and cannot change, much like a final…

SMMH
- 310
- 1
- 13
2
votes
2 answers
Rust function pointer Contravariant
I having trouble getting my head around contravariance in Rust.
Specifically:
However, the same logic does not apply to arguments. Consider trying
to satisfy:
fn handle_animal(Animal);
with
fn handle_animal(Cat);
The first function can accept Dogs,…

NimaKapoor
- 119
- 6
2
votes
1 answer
How to implement usage site variance in implicits?
Large redaction of the original question: now I present the whole code upfront, without showing the variants explaining my motivation. Apologies for the confusion.
I need a simple type class implementing a projection on one of type's member types -…

Turin
- 2,208
- 15
- 23
2
votes
1 answer
Variance/Covariance generics in Kotlin
There is a sealed-class Result, which is parameterized with two types - success result (T) and error type (R).
It is inheritered by two classes:
a. Success - data class, accepts object T in the constructor
b. Error - data class, accepts object R in…

matua
- 597
- 4
- 12
2
votes
1 answer
Kotlin generic class property
I know this may seem a repeated question but I am puzzled as to how invariance, covariance and contravariance works.
I can't understand why I can't compile this snippet:
class Test>{
lateinit var list2:List
lateinit var…

Joao Neto
- 310
- 3
- 18
2
votes
2 answers
Scala - covariant type in mutable collections
I am new in Scala world and now I am reading the book called "Scala in Action" (by Nilanjan Raychaudhuri), namely the part called "Mutable object need to be invariant" on page 97 and I don't understand the following part which is taken directly from…

Andrew_256
- 229
- 2
- 6
2
votes
0 answers
R - lavaan - sem - negative variance error
I have set up a model like this:
model3<-'
# MEASUREMENT
union =~ V24 + V25
loyality =~ V52 + V53 + V54
experience =~ V37 + V38 + V39 + V40
# STRUCTURAL
union ~ loyality
union ~ experience
# CORRELATED RESIDUALS
V37 ~~ V39
V37 ~~ V38
'
From summary…

Nneka
- 1,764
- 2
- 15
- 39
2
votes
1 answer
Scala Set, what is happening with invariant types?
While doing a refactor in a scala application I came across a situation where changing from List to Set raised a question which I didn't have before. I have some idea about variance, but I would like to understand what does it mean for the compiler…

negative
- 108
- 8
1
vote
0 answers
Why can the Swift compiler assign to a protocol-constrained type but can't when it's wrapped in another generic type?
Consider this toy example code which compiles just fine in Playground:
var x: any Hashable
let y = "String"
x = y
Then this, which also compiles fine:
var x: [any Hashable]
let y = ["String"]
x = y
While this doesn't:
struct Wrapper {
let…

Sebastian
- 904
- 1
- 8
- 16
1
vote
0 answers
Substituting the derivation of fmap and contramap with invmap
Note: The current question is a follow-up to this one. It received a great answer by Daniel Wagner.
I am trying to figure out how to derive a natural map for the given type of kind * -> *. natmap is a natural map for F, if it is equal to:
fmap ::…

Zhiltsoff Igor
- 1,812
- 8
- 24