简体   繁体   中英

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

Context

Kotlin's kotlin.String type is currently defined as follows (1.1.2) :

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

    // Operator and override function definitions.
}

Some extensions defined on kotlin.String cast the receiving instance to the java.lang.String type to forward method invocations. For example (1.1.2) :

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

However, nothing in the kotlin.String type definition makes it clear to me that this cast is guaranteed to succeed.

Questions

  • Is there any way to easily determine if any given Kotlin type is mapped to a corresponding Java type in this manner?
  • Where does this conversion actually happen? (Looking for a link to the relevant source code if possible.)

In Kotlin/JVM kotlin.String is one of the mapped types - a type that is represented with some existing JDK class in runtime.

Therefore a cast between kotlin.String and java.lang.String succeeds in runtime, even though these are two unrelated types from the point of Kotlin type system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM