In this post, I want to give you a clear explanation of type variance in Kotlin. We’ll cover the following topics:

  • introduction to variance
  • invariance, covariance, and contravariance
  • declaration-site and use-site variance

Let’s dive right in! 🤓

Introduction to variance

When talking about variance, you can think about the relation between generic types that share the same base type but have different type arguments. When working with a List<Any>, the base type would be List and the type argument Any. This relation is key to provide type safety in your API.

An important aspect to keep in mind is that while it’s safe…

Using the same language throughout your whole project can further streamline and speed up your development process. With the stable release announcement of the Kotlin DSL for Gradle, things got even more compelling for modern Android development considering Kotlin is now the preferred language for Android. The Kotlin DSL has some clear benefits in contrast to the Groovy DSL, which includes the following:

  • statically typed & type-safe DSL
  • first-class functions, extension methods
  • IDE auto-complete and content assist
  • navigating to source
  • refactoring

→ Keep in mind that the code snippets provided in this post are reduced to highlight the important changes…

André Ramon

Computer Science Student

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store