Swift 5.5 tries to grab devs’ interest with asynchronicity and concurrency features

Swift 5.5 tries to grab devs’ interest with asynchronicity and concurrency features

Apple’s programming language Swift 5.5 has been made available and comes packed with new language features especially interesting to developers looking into concurrency to speed up their applications.

One of the major additions of the release are asynchronous functions. Marked with the keyword async and called using await, they work similarly to the async functions in JavaScript. Before their introduction, programmers had to use completion closures to make sure functions weren’t blocking each other, which made for somewhat hard to read and error-prone code. Async functions are meant to help with that, along with a long list of concurrency features that also made it into the latest Swift version.

Swift 5.5 for instance adds a AsyncSequence protocol, which looks to make iterating over asynchronous value sequences as easy as looping over their synchronous equivalent. Use is therefore closely related to that of a regular Sequence, though the next() method should be an async one here. In order to facilitate the combination of synchronous code with asynchronous tasks, there are also so-called continuations which let tasks suspend themselves, but also pass on a value the synchronous code can use to get the task going again.

Since asynchronicity alone doesn’t make a concurrent programme, the Swift team also implemented a proposal for structured concurrency, which says that “all asynchronous functions run as part of an asynchronous task”. All tasks can create concurrently performing child tasks, so that there’s a hierarchy in the system and with it a way of sharing information that is supposed to make the construct “convenient to manage”. 

However, writing concurrent code isn’t the simplest of tasks. Hence there’s also a new actor entity in the update. They are similar to classes, but make sure that mutable states are only accessed by one thread at a time. Or as the proposal puts it, actors provide “the ability to use shared mutable state while still providing static detection of data races and other common concurrency bugs”.

While concurrency has made a big step, it’s not all async/await in Swift 5.5. It for instance puts an end to CGFloat conversion woes by extending the language to allow Double and CGFloat types interchangeably through implicit conversion. Other freshly-implemented proposals satisfy the demand for the application of #if to partial expressions, extend property wrappers to function and closure parameters, and expand codable synthesis support to enums and their associated values.

The update also introduces the concept of package collections, which is meant to provide educators and other community members with a way to bundle packages for specific use cases and easily share them. According to the proposal, collections are realised through static JSON documents containing lists of packages and additional metadata, which also makes them searchable.

Details on all language changes can be found in the language’s GitHub repository. It will be interesting to see if the update can get more people involved in the language, since adoption of the iOS-targeting language seems to have been stagnant — at best — of late.