How can I tell when a Kotlin type will be mapped to a Java type?

Context

The Kotlin type kotlin.String

is currently defined as follows (1.1.2) :

public class String : Comparable<String>, CharSequence {
    companion object {}

    // Operator and override function definitions.
}

      

Some extensions defined in kotlin.String

pass a receiving instance to a type java.lang.String

for forwarding method calls. For example (1.1.2) :

@kotlin.internal.InlineOnly
public inline fun String.toLowerCase(): String = (this as java.lang.String).toLowerCase()

      

However, nothing in the type definition kotlin.String

makes it clear to me that this selection is guaranteed to be successful.

Questions

  • Is it easy to figure out how a given Kotlin type maps to a corresponding Java type in this way?
  • Where is this transformation really taking place? (Look for a link to the corresponding source code if possible.)
+3


source to share


1 answer


In Kotlin / JVM, it kotlin.String

is one of the mapped types - a type that is represented by some existing JDK class at runtime.



Therefore, casting between kotlin.String

and java.lang.String

succeeds at runtime, although they are two unrelated types from the point of the Kotlin type system.

+3


source







All Articles