Skip to content

Commit

Permalink
Covariance and contravariance explanation is wrong (#3272)
Browse files Browse the repository at this point in the history
  • Loading branch information
matega authored Nov 12, 2024
1 parent 078f7ee commit b1c9386
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ interface Consumer<T> {
consume: (arg: T) => void;
}
```
Then we can use a `Consumer<Animal>` where a `Consumer<Cat>` is expected, because any function that is capable of accepting a `Cat` must also be capable of accepting an `Animal`.
Then we can use a `Consumer<Animal>` where a `Consumer<Cat>` is expected, because any function that is capable of accepting an `Animal` must also be capable of accepting a `Cat`.
This relationship is called *contravariance*: the relationship from `Consumer<T>` to `Consumer<U>` is the same as the relationship from `U` to `T`.
Note the reversal of direction as compared to covariance! This is why contravariance "cancels itself out" but covariance doesn't.

Expand Down

0 comments on commit b1c9386

Please sign in to comment.