Smallville7123
04/08/2019, 11:53 PMkotlin.collections.EmptyList cannot be cast to Kpp_gradle$1$1$1$a
fun <E> addOne(v: AbstractList<E>) {
v.add(listOf<E>() as E)
}
class a {
val empty : Int = 0
}
fun ret() {
val f = arrayListOf<a>()
f.add(a())
println("type of f is ${f.javaClass}")
println("type of f[0] is ${f[0].javaClass}")
addOne(f)
println("type of f is ${f.javaClass}")
println("type of f[1] is ${f[1].javaClass}")
abort()
}
serebit
04/08/2019, 11:56 PMSmallville7123
04/09/2019, 12:00 AMserebit
04/09/2019, 12:01 AMList<E>
to the type E
, which is pretty much guaranteed to fail. Then adding that to the passed v
listserebit
04/09/2019, 12:01 AME
is Int
, you're trying to cast a list of integers to an integerserebit
04/09/2019, 12:01 AMserebit
04/09/2019, 12:02 AMlistOf
will return a List
, which cannot be modified. mutableListOf
will return a MutableList
, which can be modifiedSmallville7123
04/09/2019, 12:10 AMSmallville7123
04/09/2019, 12:20 AMfun <E> addOne(v: AbstractList<E>) {
v.add(v[0])
}
serebit
04/09/2019, 12:20 AMSmallville7123
04/09/2019, 12:21 AMStephan Schroeder
04/09/2019, 3:04 PMSmallville7123
04/09/2019, 11:42 PMStephan Schroeder
04/10/2019, 11:55 AMSmallville7123
04/10/2019, 12:32 PMserebit
04/10/2019, 2:59 PM