In Java the java.lang package is imported by default.
In kotlin a number of functions and classes are available without being imported, like println and kotlins Array, Int, etc types.
What else is imported by default and where is it defined?

推荐答案

Kotlin stdlib有kotlin个根包和(see the full list with the content)个子包.

It seems not to be documented anywhere which of them are imported by default, but a peek into Kotlin Github sources suggests that these packages are imported for JVM target platform:

  • java.lang.*
  • kotlin.*
  • kotlin.annotation.*
  • kotlin.jvm.*
  • kotlin.collections.*
  • kotlin.ranges.*
  • kotlin.sequences.*
  • kotlin.text.*
  • kotlin.io.*
  • kotlin.coroutines.* (to be added in Kotlin 1.1, not present in 1.0.4)

我已经手动测试了它们,上面的列表适用于Kotlin 1.0.4.默认情况下,这些stdlib包是not个导入的:

  • kotlin.comparisons.*
  • kotlin.concurrent.*
  • kotlin.properties.*
  • kotlin.reflect.*
  • kotlin.reflect.jvm.*
  • kotlin.system.*

正如@Magnus所指出的,JS平台are different的默认导入.

Kotlin相关问答推荐

Lambda和普通Kotlin函数有什么区别?

Jetpack Compose中的数字 Select 器问题

Android Jetpack编写androidx.compose.oundation.lazy.grid.Items

在Spring Boot应用程序中使用网络请求功能将关键字挂起作为冗余

Spring Boot Kotlin 数据类未使用 REST 控制器中的默认值初始化

TestContainers PostgreSQLContainer 与 Kotlin 单元测试:Not enough information to infer type variable SELF

如何使用 Android CameraX 自动对焦

Kotlin JS JSON 反序列化

Koin Android:org.koin.error.NoBeanDefFoundException

从列表中的每个对象中 Select 属性

Android Studio 4.0.0 Java 8 库在 D8 和 R8 构建错误中脱糖

如果 Maybe 完成,则从其他来源将 Maybe 转换为 Single

将多个 Kotlin 流合并到一个列表中,而无需等待第一个值

Kotlin 中的内联构造函数是什么?

如何在Kotlin中创建填充空值的通用数组?

重复构建失败To use Coroutine features, you must add `ktx`......

使用导航组件在BottomNavigationView中传递参数

如何在Android Studio 4.1中默认启用Kotlin Android扩展

Kotlin协程无法处理异常

在Kotlin中创建通用二维数组