Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What packages/functions are imported by default in Kotlin?

Tags:

kotlin

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?

like image 832
Magnus Avatar asked Oct 16 '16 21:10

Magnus


People also ask

What is default package in Kotlin?

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 are imports in Kotlin?

Imports. In Kotlin, we use the import declaration to enable the compiler to locate the classes, functions, interfaces or objects to be imported. In Java, on the other hand, we can't directly import functions or methods—only classes or interfaces.

What is package level function in Kotlin?

Package-level functions are also known as top-level functions. They are declared directly inside a file without creating any class for them. They are often utility functions independent of any class: UserUtils.kt package com.app.user fun getAllUsers() { } fun getProfileFor(userId: String) { }

How do I add a package to Kotlin?

Overview. A package is a group of related classes, enums, functions, sub-packages, and so on. To import a package in Kotlin, we use the import keyword, followed by the name of the package that needs to be imported.


1 Answers

Kotlin stdlib has kotlin root package and its subpackages (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)

I've manually tested them, and the list above is true for Kotlin 1.0.4. And these stdlib packages are not imported by default:

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

As @Magnus noted, the default imports for JS platform are different.

like image 97
hotkey Avatar answered Oct 06 '22 12:10

hotkey