Nevermind, I just did some testing, and it turns out the List stays
Int
, it's just somehow a method which only accepts
Int
can take Strings and nullable values. Which I think is the real problem here.
For a
List<Int>
,
List<T>.add(element: T
, the method should only accept
Int
and not what it seems to practically accept,
Any?
, I think this is a compiler issue for not checking it properly.
import java.awt.Color
fun <T> List<T>.add1(element: T) {
if (this is MutableList<T>) { // no error
this.add(element)
}
}
val list: List<Int> = mutableListOf(1, 2)
list.add1("NaN")
list.add1(null)
list.add1(Color.BLACK)
This code should not properly compile and run, but currently does, and is somehow List<Int>, even though the list evaluates to
[1, 2, NaN, null, java.awt.Color[r=0,g=0,b=0]]