如何确定Kotlin类型何时将映射到Java类型? [英] How can I identify when a Kotlin type will be mapped to a Java type?

查看:92
本文介绍了如何确定Kotlin类型何时将映射到Java类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

科特林的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类型?
  • 这种转换实际上在哪里发生? (如果可能,寻找到相关源代码的链接.)

推荐答案

在Kotlin/JVM中,kotlin.String

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.Stringjava.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆