Why can Kotlin's code directly call the top level function from “kotlin.collections”, without import the package

Viewed 132

Why can Kotlin's code directly call the top level function from “kotlin.collections”, without import the package. such as below function listOf:

data class Person1(val name: String, val age: Int)

class DataClassExecutor {
  ... ...
  fun test(arg: String?): String? {
    val persons = listOf(
        Person1("Lucy", age = 26), 
        Person1("Lily", age = 29))
    ... ...
  }
}
1 Answers

Please refer this page: https://kotlinlang.org/spec/packages-and-imports.html. It says:

There are some packages which have all their entities implicitly imported into any Kotlin file, meaning one can access such entity without explicitly using import directives.

The List includes kotlin.collections.

This is similar to how in Java, java.lang is implicitly imported. In Java one does not need to say java.lang.System.out.println, just System.out.println is enough.

Related