Imports & Packages

A fundamental principle in programming is the acronym DRY: Don’t Repeat Yourself.

Duplicating code is not just extra work—it also creates multiple identical pieces of code that you must change whenever you make fixes or improvements. Every duplication is a place to make another mistake.

Kotlin’s import reuses code from other files. One way to use import is to specify a class, function or property name:

import packagename.ClassName import packagename.functionName import packagename.propertyName

A package is an associated collection of code; each package is usually designed to solve a particular problem, and often contains multiple functions and classes. For example, let’s import mathematical constants and functions from the kotlin.math library:

// ImportsAndPackages/ImportClass.kt import kotlin.math.PI import kotlin.math.cos // Cosine fun main(args: Array<String>) { println(PI) println(cos(PI)) println(cos(2 * PI)) } /* Output: 3.141592653589793 -1.0 1.0 */

The as keyword allows you to change a name as you import it:

// ImportsAndPackages/ImportNameChange.kt import kotlin.math.PI as circleRatio import kotlin.math.cos as cosine fun main(args: Array<String>) { println(circleRatio) println(cosine(circleRatio)) println(cosine(2 * circleRatio)) } /* Output: 3.141592653589793 -1.0 1.0 */

Sometimes you want to use multiple third-party libraries that contain classes or functions with the same name. The as keyword allows you to use these by changing names while importing. as is also useful if a library name is poorly chosen or excessively long.

You can fully qualify an import in the body of your code. In the following example, the code might be less readable due to the explicit package names, but the origin of each element is absolutely clear:

// ImportsAndPackages/FullyQualify.kt fun main(args: Array<String>) { println(kotlin.math.PI) println(kotlin.math.cos(kotlin.math.PI)) println(kotlin.math.cos(2 * kotlin.math.PI)) } /* Output: 3.141592653589793 -1.0 1.0 */

If you want to import everything from a package, use a star:

// ImportsAndPackages/ImportEverything.kt import kotlin.math.* fun main(args: Array<String>) { println(E) println(E.roundToInt()) println(E.toInt()) } /* Output: 2.718281828459045 3 2 */

Note that the kotlin.math package contains a convenient roundToInt() which rounds the Double value to the nearest integer, unlike toInt(), which simply truncates anything after a decimal point.

So far in this book we haven’t reused any code from our examples, but eventually you’ll want to write code and reuse it elsewhere. To achieve this, create your own package using the package keyword. When you use the package keyword, it must be the first non-comment statement in the file. package is followed by the name of your package, which by convention is all lowercase:

// ImportsAndPackages/PythagoreanTheorem.kt package pythagorean import kotlin.math.sqrt class RightTriangle( val a: Double, val b: Double ) { fun hypotenuse() = sqrt(a * a + b * b) fun area() = a * b / 2 }

Notice there’s no requirement to name the source-code file anything special (as there is in Java where the file name is the same as the class name).

Now the elements in the pythagorean package are available using an import:

// ImportsAndPackages/ImportPythagorean.kt import pythagorean.RightTriangle fun main(args: Array<String>) { val rt = RightTriangle(3.0, 4.0) println(rt.hypotenuse()) println(rt.area()) } /* Output: 5.0 6.0 */

Package names should be unique, and the convention is to incorporate the reversed-domain name of the package creator. Since our domain name is AtomicKotlin.com, for our package to be part of a distributed library it should be named com.atomickotlin.pythagorean rather than just pythagorean. This helps us avoid name collisions with other libraries that might also use the name pythagorean.

Kotlin allows you to choose any name for your package, but it’s considered good style for the package name to be identical to the directory name where the package files are located (this will not always be the case for the examples in this book).

Previous          Next

©2018 Mindview LLC. All Rights Reserved.