Imports
To enable classes, objects, interfaces, and functions to be used outside of the declared package we must import the required class, object, interface, or function:
import com.packt.myproject.Foo
Wildcard imports
If we have a bunch of imports from the same package, then to avoid specifying each import individually we can import the entire package at once using the *
operator:
import com.packt.myproject.*
Wildcard imports are especially useful when a large number of helper functions or constants are defined at the top level, and we wish to refer to those without using the classname:
package com.packt.myproject.constants val PI = 3.142 val E = 2.178 package com.packt.myproject import com.packt.myproject.constants.* fun add() = E + PI
Notice how the add()
function does not need to refer to E
and PI
using the FQN, but can simply use them as if they were in scope. The wildcard import removes the repetition...