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
0
votes
1 answer
Scala type variance
I have the following code
class Person
class Warrior extends Person
trait Commander[A] {
def giveOrder(to: A)
def delegate(to: Commander[A])
}
val warCommander: Commander[Warrior] = new Commander[Warrior] {
override def giveOrder(to:…

gurghet
- 7,591
- 4
- 36
- 63
0
votes
0 answers
C# Invalid variance error when having an out method parameter
I have looked at similar questions, though my case seems to be different.
For this code:
public interface MyInterface {
bool TrySomething(out T val);
}
I get this error on C# 7.2:
Invalid variance: The type parameter 'T' must be…

Fit Dev
- 3,413
- 3
- 30
- 54
0
votes
2 answers
Covariance confusion. Can't assign tuples of implemented interfaces to list of tuples
Preface: I am aware that there are a lot of questions and answers about covariance and contravariance but I'm still feeling muddled and not sure what solution to implement.
I have two interfaces whose implementations are intended to be used together…

Zac
- 1,722
- 1
- 19
- 22
-1
votes
1 answer
C# generic inheritance: invariance getting in the way of calling derived class
What I want to do:
abstract class TileBase
{
protected TileGroup tileGroup;
}
class Tile : TileBase
{
public Tile(Province Province)
{
tileGroup = Province;
}
}
abstract class TileGroup
{
protected T[]…

Michiel
- 57
- 1
- 6