diesieben07
06/25/2018, 9:28 AMMutableList<Object>
is a MutableList<in CharSequence>
because you can put any CharSequence
into a list of objects.paulex
06/25/2018, 10:31 AMdiesieben07
06/25/2018, 10:38 AMval
cannot be reassigned: Both are not correct.paulex
06/25/2018, 10:43 AMopen class Animal{
private var id:Int = 0
}
data class Cat(
private var name:String = ""
):Animal()
interface ViewInterface<in T:Animal> {
fun setAnimal(animal:T)
}
fun main(args:Array<String>){
var x:ViewInterface<Cat> = object:ViewInterface<Cat>{
override fun setAnimal(s:Cat){
println("Cat was set")
}
}
var y:ViewInterface<Animal> = object:ViewInterface<Animal>{
override fun setAnimal(s:Animal){
println("Animal was set")
}
}
// x.setAnimal(Animal())
x = y //*This works*
// y = x //This doesnt
// x.setAnimal(Animal())
}
diesieben07
06/25/2018, 10:45 AMx
is of type ViewInterface<Cat>
. Since ViewInterface
uses declaration-site variance, this is really ViewInterface<in Cat>
and ViewInterface<Animal>
is assignable to that.Animal
(your y
) it can also take in a Cat
.ViewInterface<in Cat>
means "anything that can take a Cat". And your y
fits that, it can even take any animal, which includes cats.paulex
06/25/2018, 10:54 AMy = x
// fail
y
is animal
and i would expect that it to take take a cat x
however if it was covariant
y = x
is acceptable
i know im missing a point buh i'm hoping to get itdiesieben07
06/25/2018, 10:54 AMy
is ViewInterface<in Animal>
, i.e. "i can take animals". But x
can only take cats, not any animal.paulex
06/25/2018, 12:43 PM