arXiv · 2609.28822
Pack Iteration in Swift: Ordinary Control Flow for Variadic Generics
Abstract
Variadic generics are a powerful tool for type-safe meta-programming. Yet in most widely used languages, they remain an "expert-only" feature due to their reliance on complex patterns such as recursive decomposition or expansion expressions that do not compose naturally with ordinary control flow. In C++, for example, accessing elements of parameter packs has traditionally relied on unintuitive recursive patterns that "peel off" elements. This paper presents Pack Iteration, a feature introduced in Swift 6.0 that allows developers to iterate over parameter packs using a familiar, imperative for-in loop. By treating pack expansion as a first-class source for iteration, Swift bridges the gap between high-level expressiveness and advanced generic programming. We detail the design and implementation of this feature within the Swift compiler, focusing on the challenges of bridging static type-checking in the constraint system with dynamic execution in the Swift Intermediate Language. Unlike traditional models that expand packs at compile time, Swift's implementation supports on-demand evaluation, enabling efficient dynamic iteration and short-circuiting control flow. Our empirical evaluation confirms that pack iteration provides performance comparable to - and sometimes significantly better than - the complex workarounds previously required.
Explore related subjects
Keep this discovery
Explore connections, maps & timelines
Serafima Nerush, Eitan Frachtenberg. 2026-09-23. Pack Iteration in Swift: Ordinary Control Flow for Variadic Generics. https://arxiv.org/abs/2609.28822
Cite the original work for its findings. Save a collection to share your selection of sources.