Explain Kotlin's declaration-site variance with in and out

This tests your grasp of generic type safety and API design. Explain that `out` marks covariant producers (e.g., `List<out E>`) and `in` marks contravariant consumers, then contrast this cleaner declaration-site model with Java's repetitive use-site wildcards.
This tests your understanding of generic type safety and API design trade-offs. A great answer defines covariance (`out T`, producers) and contravariance (`in T`, consumers), explaining how they guarantee type safety. You must then contrast Kotlin's declaration-site variance, where the API author specifies variance once, with Java's use-site wildcards (`? extends T`), which burden the API consumer. The key is showing *why* Kotlin's model leads to cleaner, more intuitive APIs. A red flag is simply defining the terms without this context.
Read the original → kotlinlang.org
- #kotlin
- #generics
- #api design
- #type system
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.