I know this may seem a repeated question but I am puzzled as to how invariance, covariance and contravariance works.
I can't understand why I can't compile this snippet:
class Test<X: List<Any>>{
lateinit var list2:List<Any>
lateinit var list1:X
fun putList(){
list2 = emptyList()
list1 = emptyList<Any>()
}
}
I get the type mismatch error Required X found List
However if I define the X generic as 'out' I will get a error indicating that the parameter X is invariant in list1 var.
Can someone help another poor soul lost in Kotlin generics?