0Pricing
Kotlin Academy · Lesson

Use-Site Variance

Type projections.

Use-Site Variance

Sometimes a generic class is invariant overall, but a specific function only produces or only consumes the type. Use-site variance (type projections) applies out or in right at the place a type is used.

Why Use-Site Variance

An invariant class like MutableList<T> cannot be made covariant at declaration because it both reads and writes T. But a particular function might only read from it, and projections let us express that safely.

All lessons in this course

  1. Generic Functions and Classes
  2. Declaration-Site Variance
  3. Use-Site Variance
  4. Generic Constraints
← Back to Kotlin Academy