Skip to content

Commit

Permalink
chore: clarify covariance example
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahhaggarty committed Sep 6, 2023
1 parent 22b3747 commit 85d1d47
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion docs/topics/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ In other words, the wildcard with an _extends_\-bound (_upper_ bound) makes the
The key to understanding why this works is rather simple: if you can only _take_ items from a collection,
then using a collection of `String`s and reading `Object`s from it is fine. Conversely, if you can only _put_ items
into the collection, it's okay to take a collection of `Object`s and put `String`s into it: in Java there is
`List<? super String>`, a _supertype_ of `List<Object>`.
`List<? super String>`, which accepts `String`s or any of its supertypes.

The latter is called _contravariance_, and you can only call methods that take `String` as an argument on `List<? super String>`
(for example, you can call `add(String)` or `set(int, String)`). If you call something that returns `T` in `List<T>`,
Expand Down

0 comments on commit 85d1d47

Please sign in to comment.