Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Kotlin provides an import aliasing mechanism via the as keyword, allowing developers to locally bind a new identifier to a fully qualified class, function, property, or object at the point of import. This creates a file-scoped alternative identifier for the imported declaration without altering the original source code.

Syntax

The aliasing syntax appends the as keyword followed by the desired local identifier to a standard import directive:
import package.name.OriginalIdentifier as LocalAlias

Technical Mechanics

  • Lexical Scoping: An import alias is strictly confined to the file in which it is declared. It does not pollute the global namespace or affect how the target declaration is referenced in other files within the same module.
  • Compiler Resolution: The alias is a purely syntactic construct handled during the compiler’s import resolution phase. The Kotlin compiler maps the LocalAlias directly to the OriginalIdentifier’s fully qualified name. It does not generate wrapper classes or incur runtime overhead.
  • Applicability: The as keyword can be applied to any importable symbol. This includes:
    • Classes and Interfaces
    • Top-level functions
    • Top-level properties
    • Object declarations
    • Enum constants

Code Examples

Aliasing Classes
import java.util.Date as UtilDate
import java.sql.Date as SqlDate

// The aliases are instantiated directly as if they were the original class names
val currentUtilDate: UtilDate = UtilDate()
val currentSqlDate: SqlDate = SqlDate(System.currentTimeMillis())
Aliasing Top-Level Functions
import kotlin.math.max as findMaximum

// The function is invoked using the local alias
val result = findMaximum(10, 20)

Distinction from typealias

It is critical to distinguish an import alias (import ... as) from a type alias (typealias):
Characteristicimport ... astypealias
ScopeFile-level only.Package, module, or public scope (based on visibility modifiers).
TargetAny importable symbol (Types, Functions, Properties, Objects).Types only (Classes, Interfaces, Function Types).
Declaration LocationFile header, alongside standard imports.Top-level declaration within a file.
ReusabilityCannot be referenced outside the declaring file.Can be imported and used by other files/modules.
Master Kotlin with Deep Grasping Methodology!Learn More