如何确定Kotlin类型何时将映射到Java类型? [英] How can I identify when a Kotlin type will be mapped to a Java type?
问题描述
科特林的kotlin.String
类型为在kotlin.String
上定义的某些扩展将接收实例强制转换为java.lang.String
类型以转发方法调用. 例如(1.1.2):
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()
但是,kotlin.String
定义类型 中没有任何内容使我清楚地知道此强制转换肯定会成功.
However, nothing in the kotlin.String
type definition makes it clear to me that this cast is guaranteed to succeed.
- 是否可以通过这种方式轻松确定是否将任何给定的Kotlin类型映射到相应的Java类型?
- 这种转换实际上在哪里发生? (如果可能,寻找到相关源代码的链接.)
推荐答案
In Kotlin/JVM kotlin.String
is one of the mapped types - a type that is represented with some existing JDK class in runtime.
因此,即使从Kotlin类型系统的角度来看,这是两个不相关的类型,也可以在运行时成功地在kotlin.String
和java.lang.String
之间进行强制转换.
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.
这篇关于如何确定Kotlin类型何时将映射到Java类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!