out
here restricts us from calling add
fun <T> test1(list: MutableList<out T>) {
list.add(list[0])
}
error (from old version of kotlin which gave better errors):
Out-projected type 'MutableList<out T>' prohibits the use of 'public abstract fun add(element: E): Boolean defined in kotlin.collections.MutableList'
however, why does
in
not restrict us from calling
removeAt
here?
fun <T> test2(list: MutableList<in T>) {
// public fun removeAt(index: Int): E
list.removeAt(0)
}
it does work correctly when
in
is used at declaration site -
class SomeList<in T> {
public fun removeAt(index: Int): T { TODO() }
}
Type parameter T is declared as 'in' but occurs in 'out' position in type T