Skip to content

Instantly share code, notes, and snippets.

@magdamiu
Created June 20, 2020 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magdamiu/08842e3d8dfcff4691fc87a6df220f51 to your computer and use it in GitHub Desktop.
Save magdamiu/08842e3d8dfcff4691fc87a6df220f51 to your computer and use it in GitHub Desktop.
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