liveBook · Manning

Covariance

Covariance is a type-system property in which subtyping of a type argument induces the same-direction subtyping relationship for its parameterized type.

How 3 Manning books explain it

Kotlin in Action, 2E

Covariance is a generic type relationship in which subtyping between parameterized types preserves the subtyping relationship between their type arguments.

How it is used
Enables read-only generic APIs to accept subtype-specialized values where a supertype-specialized value is required.
How deep it goes
Defines preserved subtyping and the out-position safety rule, while only summarizing use-site variance.
Examples
Uses Producer<out T>, Herd<Cat> passed to Herd<Animal>, and Kotlin’s read-only List.
What you can do after
Apply it by marking producer-only generic type parameters out and recognizing safe subtype substitution.

Read how Kotlin in Action, 2E explains covariance →

Fabulous Adventures in Data Structures and Algorithms

Covariance is a type-system property that permits a generic type instantiated with a derived reference type to be treated as the same generic type instantiated with a base reference type.

How it is used
Explains safe substitution between generic interfaces when their varying type is exposed only for reading.
How deep it goes
Builds safety from mutation counterexamples, declaration constraints, immutable stacks, and assignment-compatibility morphisms.
Examples
Uses IEnumerable<Giraffe>, unsafe IList<Tiger> conversion, a covariant immutable stack, and a four-type assignment category.
What you can do after
Evaluate generic conversions by checking variance direction, input capabilities, reference types, and output-position usage.

Read how Fabulous Adventures in Data Structures and Algorithms explains covariance →

Functional Programming in Scala, Second Edition

Covariance is a type-system property in which subtyping of a type argument induces the same direction of subtyping for the parameterized type.

How it is used
Uses covariance to make functional data-structure and stream-processing APIs compose more naturally with subtype relationships.
How deep it goes
Defines covariance, contrasts invariance, and works through List, Nil, Nothing, and Pull without developing variance rules internally.
Examples
Uses List[Dog] versus List[Animal], Nil as multiple list types, covariant Pull, and Parser[+_] as concrete Scala examples.
What you can do after
Evaluate/understand when a Scala type parameter should be covariant and predict resulting subtype relationships.

Read how Functional Programming in Scala, Second Edition explains covariance →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.