Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News Swift 5.7 Brings New Generics Implementation and Reference Counting Improvements

Swift 5.7 Brings New Generics Implementation and Reference Counting Improvements

Announced at WWDC 2022, Swift 5.7 is now officially available. It includes major improvements to the compiler internals, and many syntax and standard library additions, including String regex, concurrency updates, and more.

While not immediately visible to the programmer, the improvements to Swift's generics implementation and to automatic reference counting aim to contribute to program performance and correctness. Speaking of generics, work has focused on reimplementing the type checker:

The new implementation fixes many long-standing bugs, mostly related to the handling of complex same-type requirements, such as same-type requirements on a collection’s SubSequence associated type, and code making use of the CaseIterable protocol which defines the requirement Self.Element == Self.

Generics also get a number of improvements at the syntax level. In particular, you can now specify an associated type in a protocol declaration, for example:

protocol Contestant<Habitat> {
    associatedtype Habitat: Territory

    var home: Habitat { get set }

struct SeaHabitat : Territory {

struct ConcreteContestant : Contestant {
    var home: SeaHabitat

The Habitat associated type is the primary type for the protocol, i.e., the one that is most commonly specified at the call site. This also opens the possibility of using opaque types with protocols and generics, for example in an object constructor that hides the concrete type of Contestant that is returned:

func currentContestant() -> some Contestant {
    ConcreteContestant(home: SeaHabitat(...))

This extension applies to existing protocols, too, allowing you to use Sequence<String> to represent and kind of sequence of strings irrespective of their actual implementation, be it an Array, a Dictionary, and so on.

As mentioned, Swift 5.7 also improves automatic reference counting, making it more predictable and robust to compiler optimizations thanks to new rules to shorten variable lifetimes. In particular, programmers will not need to use withExtendedLifetime() anymore, when evaluating a closure whose correctness depends on a given variable, to prevent the compiler from optimizing it away before the closure returns.

Support for concurrency has matured in Swift 5.7 with better actor isolation, which aims to reduce the chance of data race and makes possible a new strict concurrency checker in Xcode. Additionally, Swift 5.7 also introduces the Async Algorithms package to provide ready-made solutions for a number of AsyncSequence use cases.

One improvement to the language syntax which will be greatly appreciated by all Swift developers, is the new shorthand for optional unwrapping in conditionals, which allows to more succinctly write:

var startDate: Date?
if let startDate {
    let newDate = startDate.addTime(...)

As a final mention, Swift 5.7 also bring a number of refinements to Windows support, including a more robust installer, improved support for static libraries, streamlined SDK layout, and the new Swift for Visual Studio Code extension.

Swift 5.7 includes many additional features and extensions than what can be covered here, so make sure you read the official announcement for the full detail.

About the Author

Rate this Article