Problem Statement
What is the difference between 'out' and 'in' variance modifiers in Kotlin generics?
Explanation
The out modifier makes a generic type covariant, meaning it can only be produced or returned but not consumed as a parameter, like List less than out T greater than allows passing List less than String greater than where List less than Any greater than is expected. Use out for types that produce values, following the producer pattern.
The in modifier makes a type contravariant, allowing consumption but not production, useful for types that only take values as input like Comparable less than in T greater than. This enables passing Comparable less than Any greater than where Comparable less than String greater than is expected.
Use-site variance with out and in provides flexibility when declaration-site variance isn't specified, enabling safe type relationships that would otherwise cause compile errors, and the compiler enforces that out types can't be used in input positions and in types can't be used in output positions.