I'm studying this extension method in JavaUtil.kt
``
inline fun <T> java.util.Collection<T>.toArray() : Array<T> {
val answer = Array<T>(this.size)
var idx = 0
for (elem in this)
answer[idx++] = elem
return answer as Array<T>
}
because I am finding that these two ArrayList<String> are not equal:
``
val a = arrayList(“a”, “b”, “aa”)
val b = arrayList(a.toArray())
In the IDE, the “as” in the extension function return statement is browned-out, with a tooltip that says “This cast can never succeed”. I don’t understand why the “as Array<T>” is needed at all. When I remove it, the “answer” in “return answer” goes red, with a error that says “inferred type was Array<T?> but expected Array<T>”.
Why is the “as Array<T>” needed, and why is the inferred type Array<T?> if I remove it?