Skip to main content
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:

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
Aliasing Top-Level Functions

Distinction from typealias

It is critical to distinguish an import alias (import ... as) from a type alias (typealias):
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More