Hello everyone, does anyone know, why the first f...
# announcements
z
Hello everyone, does anyone know, why the first four functions return false?
Copy code
fun main() {
    fun main() {
    val arr1 = Array(0) { "" }::class
    val arr2 = arrayOf("")::class
    val arr3 = Array::class

    println(arr1 == Array::class)               //false
    println(arr1.isSubclassOf(Array::class))    //false
    println(arr2 == Array::class)               //false
    println(arr2.isSubclassOf(Array::class))    //false
    println(arr3 == Array::class)               //true
    println(arr3.isSubclassOf(Array::class))    //true
}
I think thats a bug.
s
Why dont you inspect
arr1
and it might become obvious
z
Did that, all have the qualified name kotlin.Array And all of them should be of class kotlin.Array.
s
Copy code
val arr1: KClass<out Array<String>>
val arr2: KClass<out Array<String>>
val arr3: KClass<Array<*>>
z
Is it somewere documented that there is no type erasure in generics like array or lists in JVM? Thats quite inconvenient, especially when you are working with a commons library.
btw. that doesn't change my comment. If both KClasses are from type kotlin.Array they shoud return true when you call
equals
. Especially when you cant call
isArray
nvm. found the issue
s
I think you have multiple misunderstandings. Maybe what you want to test is assignability between the types which is not limited to subclass of and equal class objects.
z
No, i just want to check if an object instance is of the type Array in my MPP project in my commons code. This is a simplified example of my code:
Copy code
inline fun <reified T> T.call(block: (T)->Unit){
    if (T::class == Array::class){
        error("This is an array")
    }
    block(this)
}
In javascript that code works as expected, but when i use the same function in my JVM code it does not work as expected.
that error occurs due to the fact, that in the JVM the arrays are mapped to their JVM counterparts, which are not subject to type erasure. -> They have different Class tokens.