A practical Swift package demonstrating how to use @frozen and @inlinable attributes to write stable and high-performance public APIs.
These annotations are critical for library developers who care about ABI stability, performance optimization, and clean module interfaces.
Marks a public enum or struct as layout-stable. Once frozen:
- New cases/properties cannot be added.
- Enables exhaustive
switchstatements in external modules without@unknown default. - Helps the compiler optimize memory and dispatch.
Marks a function or computed property as inlinable across module boundaries. This allows:
- Clients to see and compile the body of the function.
- The compiler to apply inline optimizations.
- Reduced call overhead in performance-sensitive APIs.
InlinableAndFrozenEnum/ ├── README.md ├── PaymentResult.swift /// @frozen enum └── MessageHelper.swift /// @inlinable function
| Scenario | Use @frozen |
Use @inlinable |
|---|---|---|
| Building a public enum with fixed cases | ✅ | ❌ |
| Struct with known and stable layout | ✅ | ❌ |
| Public helper method needing performance | ❌ | ✅ |
| Exposing implementation to other modules | ❌ | ✅ |
Here are some official Swift resources to deepen your understanding of @frozen, @inlinable, and ABI stability:
-
SE-0192: Non-Exhaustive Enums
Discusses the implications of frozen vs non-frozen enums in public Swift APIs. -
SE-0193: Cross-module Inlining and Specialization
Introduces the@inlinablekeyword and its benefits for performance and cross-module optimization. -
Swift Blog: Library Evolution in Swift
Explains how ABI and module stability are handled in Swift, and how attributes like@frozenfit in.