arXivDaily arXiv每日学术速递 周一至周五更新
arXiv周末暂无论文更新,休息一下吧,周末愉快~~

Swift 中的包迭代:可变泛型的普通控制流

Pack Iteration in Swift: Ordinary Control Flow for Variadic Generics

Serafima Nerush, Eitan Frachtenberg

arXiv 2609.28822首次发表:更新:

发表机构

Reed College(里德学院)

机构由 AI 辅助整理,请以论文原文为准。

AI 中文总结

本文介绍 Swift 6.0 的包迭代功能,通过 for-in 循环实现可变泛型的普通控制流,支持按需求值,性能优于传统复杂变通方法。

AI 中文摘要

可变泛型是类型安全元编程的强大工具。然而,在大多数广泛使用的语言中,由于它们依赖于递归分解或展开表达式等复杂模式,这些模式无法与普通控制流自然组合,因此它们仍然是“专家专用”功能。例如,在 C++ 中,访问参数包的元素传统上依赖于不直观的递归模式,这些模式会“剥离”元素。本文介绍了包迭代(Pack Iteration),这是 Swift 6.0 中引入的一项功能,允许开发人员使用熟悉的命令式 for-in 循环对参数包进行迭代。通过将包展开视为迭代的一等来源,Swift 弥合了高级表达性与高级泛型编程之间的差距。我们详细介绍了该功能在 Swift 编译器中的设计和实现,重点关注在约束系统中桥接静态类型检查与 Swift 中间语言中动态执行的挑战。与在编译时展开包的传统模型不同,Swift 的实现支持按需求值,从而实现高效的动态迭代和短路控制流。我们的实证评估证实,包迭代提供的性能与之前所需的复杂变通方法相当,有时甚至显著优于它们。

英文摘要

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.

论文原文

arXiv 摘要页 · PDF 原文 · HTML 原文

↑