Generic Variance — Fundamental Principles
Demystifying variance once and for all — explaining the fundamental principles of variance from the bottom up, using assignability of functions.
— — — — — — — — — — — — — — —
THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.
— — — — — — — — — — — — — — —
Tags: #FYI++
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.
The basic question we posed in the previous article is this: Can we, and when can we, take code (a class, a function) that was generated (using generics) for a Dog
type and use it in place of code generated for an Animal
type? Can we vary the generic type? And if so, under what conditions? More technically, when there is a sub-/super-type relationship…