Covariance, Contravariance, Invariance

Defining covariance, contravariance and invariance, declaration site variance vs. use site variance (type projections) and the in and out keywords

Gabriel Shanahan
The Kotlin Primer
Published in
6 min readSep 16, 2022

--

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI #KOTLIN FEATURE

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

Let’s recap what we found out in the previous chapters:

  • Variance deals with the ability to transfer sub-/super-type relationships between types to sub-/super-type relationships between generic constructs involving those types. In practice, this means asking questions like “If Int is a subclass of Number, can I, for

--

--