Problem Statement
Explain variance in Kotlin generics including declaration-site and use-site variance with examples.
Explanation
Declaration-site variance uses out for covariant types that only produce values like interface Producer less than out T greater than, and in for contravariant types that only consume values like interface Consumer less than in T greater than. This variance is specified at the interface or class level and applies everywhere.
Use-site variance applies variance at the point of use with out or in modifiers like function less than T greater than process open paren list colon List less than out T greater than close paren, useful when the type itself isn't variant but you want variance in specific contexts. Star projection less than asterisk greater than represents unknown type when variance doesn't matter.
Covariance with out allows passing List less than String greater than where List less than Any greater than is expected because String is subtype of Any, but you can only read from such lists not add to them. Contravariance with in allows passing Comparator less than Any greater than where Comparator less than String greater than is expected because you can compare Strings using Any comparator.
Type constraints with colon limit generic types like less than T colon Number greater than requiring T to be Number or subtype, and where clauses support multiple constraints. Understanding variance prevents type errors and enables writing flexible generic code.
