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.

A declaration import in Swift is a targeted import statement that brings a single, specific symbol from an external module into the current file’s unqualified namespace, rather than exposing the module’s entire public API to unqualified lookup. While it restricts unqualified access to the designated declaration, it also brings the module’s name into scope, allowing access to other public symbols via fully qualified name lookup.

Syntax

The syntax requires specifying the kind of declaration being imported, followed by the fully qualified path to the symbol:
import <import-kind> <ModuleName>.<DeclarationName>

Supported Declaration Kinds

The <import-kind> keyword generally corresponds to the underlying type of the symbol being imported. The Swift compiler recognizes the following valid import kinds:
  • class
  • struct
  • enum
  • protocol
  • typealias
  • func
  • let
  • var
  • macro
While the import kind typically matches the declaration exactly, the typealias keyword serves as a universal type import. It can be used to successfully import any nominal type. Because Swift does not possess an actor import kind, developers must use import typealias to target an actor declaration.

Technical Mechanics

Namespace Isolation and Qualified Lookup When a declaration import is evaluated, the Swift compiler binds the specified identifier to the current file’s scope for unqualified lookup. If code within the file attempts an unqualified reference to any unimported symbol from the same module, the compiler will emit an unresolved identifier error. However, because the import also brings the module name itself into scope, developers can still access any unimported public symbol from that module using a fully qualified reference (e.g., ModuleName.UnimportedSymbol). Extension Leakage A critical characteristic of Swift’s import system is that declaration imports do not isolate or hide extensions. Importing a specific declaration from a module automatically brings all extensions defined within that module into the file’s global scope. For example, importing a single class from Foundation will still expose all of Foundation’s extensions on standard library types (like String or Int) to the current file. Name Resolution and Shadowing Declaration imports take precedence during unqualified name lookup. If multiple modules define a symbol with the identical name, a declaration import explicitly instructs the compiler which module’s symbol to bind to the identifier, resolving potential ambiguity at the file level. Member Visibility and Top-Level Restriction Importing a composite type (such as a class, struct, or enum) automatically exposes its accessible members (properties, methods, and nested types) to the current file. The declaration import restricts the global namespace but does not alter the internal access control of the imported type itself. Swift’s declaration imports only support top-level module or submodule symbols; nested types and static members cannot be targeted directly by a declaration import.

Syntax Visualization

// Imports only the RunLoop class for unqualified lookup
import class Foundation.RunLoop

// Unqualified lookup succeeds for the imported declaration
let loop = RunLoop.current

// Unqualified lookup fails for unimported declarations (Compiler Error)
// let data = Data() 

// Fully qualified lookup succeeds for unimported declarations
let data = Foundation.Data()

// Acts as a universal nominal type import (used here to import an actor)
import typealias _Concurrency.MainActor

// Imports only the sin function from the Darwin module
import func Darwin.sin

// Imports only a specific global constant
import let Foundation.NSNotFound

// Imports a specific protocol
import protocol SwiftUI.View

// Imports a specific macro (Swift 5.9+)
import macro Swift.OptionSet
Master Swift with Deep Grasping Methodology!Learn More