I am very confused about mapped types. Also see this issue I have reported.
/**
* This demo shows that Kotlin types and their mapped Java types are paradoxically the same and distinct
* at the same time - at compile time.
*/
fun demo() {
val ks: kotlin.String = ""
val js: java.lang.String = "" as java.lang.String
val kc: kotlin.collections.Collection<Int> = listOf()
val jc: java.util.Collection<Int> = listOf<Int>() as java.util.Collection<Int>
// Kotlin type cannot access Java method java.lang.String.getBytes() (OK)
js.getBytes() // Works (OK)
js.bytes // (Property access syntax) Works (OK)
ks.getBytes() // Compile error: "Unresolved reference: getBytes" (OK?)
// Java type can access Java method java.lang.String.length() without parentheses. (STRANGE)
// Or is it the property kotlin.String.length and IntelliJ is wrong?
ks.length // kotlin.String.length - Works (OK)
js.length // java.lang.String.length() - Works (STRANGE)
// These do not exist, so the above problems have nothing to do with property access syntax:
ks.getLength() // Compile error: "Unresolved reference getLength()"
js.getLength() // Compile error: "Unresolved reference getLength()"
// Java type can access Kotlin extension property kotlin.CharSequence.indices (STRANGE)
ks.indices // Works (OK)
js.indices // Works (STRANGE)
// Kotlin type can access Java default method java.util.Collection.parallelStream() (STRANGE)
jc.parallelStream() // Works (OK)
kc.parallelStream() // Works (STRANGE)
// =========================================================================================================
// kotlin.String::class === java.lang.String::class
assert(kotlin.String::class === java.lang.String::class) // Works (STRANGE)
ks.getBytes() // But then this should work too, but it doesn't.
js.length // It would explain why this works, but CTRL-Click then should navigate to kotlin.String.length, not java.lang.String.length().
assert(kotlin.String::class.isSubclassOf(kotlin.CharSequence::class)) // Of course this works...
assert(java.lang.String::class.isSubclassOf(kotlin.CharSequence::class)) // so this works too...
js.indices // ... and explains why this works.
// =========================================================================================================
// kotlin.collections.Collection::class === java.util.Collection::class
assert(kotlin.collections.Collection::class === java.util.Collection::class) // Works (STRANGE)
kc.parallelStream() // But explains why this works.
}
Can someone please explain this to me?