In this post, I want to give you a clear explanation of type variance in Kotlin. We’ll cover the following topics:
Let’s dive right in! 🤓
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 to pass a Cat to a function that expects Any, it’s not always safe to pass a List<Cat> when a List<Any> is expected. …
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:
→ Keep in mind that the code snippets provided in this post are reduced to highlight the important changes in their given context and do not represent complete files. …