Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Covariance in Kotlin
class CovarianceSample<T>
fun main() {
val firstSample: CovarianceSample<Any> = CovarianceSample<Int>() // Error: Type mismatch
val secondSample: CovarianceSample<out Any> = CovarianceSample<String>() // OK , String is a subtype of Any
val thirdSample: CovarianceSample<out String> = CovarianceSample<Any>() // Error: Type mismatch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.