kotlin - Kotlin 中的单个感叹号

在 Kotlin 中,一个感叹号是什么意思?我见过几次,尤其是在使用 Java API 时。但我在文档和 StackOverflow 上都找不到它。

最佳答案

他们被称为 platform types他们的意思是 Kotlin 不知道该值是否可以为 null 并且由您决定它是否可以为空。

In a nutshell, the problem is that any reference coming from Java may be null, and Kotlin, being null-safe by design, forced the user to null-check every Java value, or use safe calls (?.) or not-null assertions (!!). Those being very handy features in the pure Kotlin world, tend to turn into a disaster when you have to use them too often in the Kotlin/Java setting.

This is why we took a radical approach and made Kotlin’s type system more relaxed when it comes to Java interop: now references coming from Java have specially marked types -- Kotlin Blog

https://stackoverflow.com/questions/43826699/

相关文章:

enums - Kotlin 中具有反向查找的有效枚举?

kotlin - Kotlin 中有构造函数引用吗?

generics - 如何在 Kotlin 中将 TypeToken + 泛型与 Gson 一起使用

syntax - Kotlin 二级构造函数

kotlin - Kotlin 中等效的 Swift 'if let' 语句

kotlin - Kotlin 中的多变量 let

generics - Kotlin 中的 reified 关键字是如何工作的?

kotlin - Kotlin中折叠和减少之间的区别,何时使用?

function - 何时在 Kotlin 中使用内联函数?

loops - Kotlin 中 `break` 中的 `continue` 和 `forEach`