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

WHAT IT TESTS: Understanding declaration-site variance versus wildcards. ANSWER OUTLINE: out T means covariant producer (read), in T means contravariant consumer (write), removing wildcard noise. RED FLAG: Confusing in/out with bounds or claiming immutability.
WHAT IT TESTS: Whether you reason about variance at the declaration level instead of every call site with wildcards. ANSWER OUTLINE: A strong answer defines out T as covariance for producers that only return T, so Source<String> is a subtype of Source<Object>; defines in T as contravariance for consumers that only accept T, so Comparable<Object> is a subtype of Comparable<String>; and notes Kotlin pushes this to the API author once rather than forcing ? extends or ? super everywhere.
Read the original → kotlinlang.org
- #kotlin
- #generics
- #variance
- #type-system
- #api-design
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.