GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 suggests that in the future we might see a variant of Swift's zip() function that supports any number of sequences.

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.