Imports in Kotlin

From within the same module:

package academy.learnprogramming.inheritance.inheritance // We can change this, but suggest sticking to directory structure

// Packages don't have to match the directory structure, but best to
// We import classes and interfaces as we would in Java,  but what about top-level functions?
fun topLevel(str: String) = println("Top-level function: $str") // This top-level function is called from a file in a different package. If auto-import is turned on it will detect this function and import for us
package academy.learnprogramming.inheritance.academy.learnprogramming.anotherpackage

import academy.learnprogramming.inheritance.inheritance.topLevel // Import statement produced by IDE

// enums can be imported as a class or individual enum:
import academy.learnprogramming.inheritance.inheritance.Department.* // class, or
import academy.learnprogramming.inheritance.inheritance.Department.IT // enum

// If importing from multiple sources and two have the same name you can import 'as':
import academy.learnprogramming.inheritance.inheritance.someImport as anotherImportName // Use this reference when calling. Does not work with the * wildcard

fun main() {
    topLevel("A string") // IDE suggests this function and imports it as auto-import is turned on
    println(IT.getDeptInfo())
}
// Object declarations can accessed similarly
// If trying to access class functions you will need to import the entire class
// typealiases and extension functions are imported the same way, by name

To add another module you must use the IDE. Right-click on the module and select Open Module Settings (F4). Make sure the correct module is selected and use the ‘+’ to add Module Dependency:

You can then use the Alt-Enter/Cmd-Enter prompt to auto-import the function you have typed in, or the IDE will suggest the function name if you start typing it.