WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Pack iteration

Available from Swift 6.0

Paul Hudson      @twostraws

SE-0408 introduces pack iteration, which adds the ability to loop over the parameter pack feature introduced in Swift 5.9.

Although value packs remain one of the most complex features of Swift, the evolution proposal shows just how useful this feature is by adding tuple comparison for any arity in just a few lines of code:

func == <each Element: Equatable>(lhs: (repeat each Element), rhs: (repeat each Element)) -> Bool {
    for (left, right) in repeat (each lhs, each rhs) {
        guard left == right else { return false }
    return true

If that means nothing to you, the Simple English version is that SE-0015 added support for direct tuple comparison up to arity 6, meaning that two tuples with up to six items could be compared using ==. If you tried comparing tuples with seven items – e.g. (1, 2, 3, 4, 5, 6, 7) == (1, 2, 3, 4, 5, 6, 7) – Swift would throw up an error. SE-0408, along with the code above, removes that restriction.

Tantalizingly, the Future Directions section of this evolution proposal suggest that in the future we might see a variant of Swift's zip() function that supports any number of sequences.

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Other changes in Swift 6.0…

Download all Swift 6.0 changes as a playground Link to Swift 6.0 changes

Browse changes in all Swift versions

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.