This article is more than 1 year old

Swift 5.5 unleashed with async keyword to fix 'pyramid of doom', plus other changes in 'massive release'

Full concurrency model at last

Apple has emitted version 5.5 of the Swift programming language, described as a "massive release," including async/await keywords, package collections, and improved Objective-C interoperability.

The most anticipated new features in Swift 5.5 are around concurrency.

One of the issues with Swift's earlier approach to concurrency was that when several asynchronous operations were written by a developer, the result would be a "pyramid of doom" – nested calls that were hard to follow, and which made flow control and error handling problematic "because Swift's natural error handling mechanism cannot be used," as the language's inventor Chris Lattner put it.

The solution was to adopt the async/await pattern as seen in Microsoft's C# (inspired by asynchronous workflows in F#) and adopted by other languages including JavaScript and Python. Swift now supports code like this:

func loadWebResource(_ path: String) async throws -> Resource
func decodeImage(_ r1: Resource, _ r2: Resource) async throws -> Image
func dewarpAndCleanupImage(_ i : Image) async throws -> Image

func processImageData() async throws -> Image {
  let dataResource  = try await loadWebResource("dataprofile.txt")
  let imageResource = try await loadWebResource("imagedata.dat")
  let imageTmp      = try await decodeImage(dataResource, imageResource)
  let imageResult   = try await dewarpAndCleanupImage(imageTmp)
  return imageResult

In addition, anonymous closures are implicitly async if they contain an await expression.

This new concurrency model will also improve interoperability with Objective-C, Apple's older language. Objective-C has completion-handler methods which will be translated into async methods in Swift, and equally Swift async methods can be exported as completion-handler methods.

A second key part of the Swift concurrency model is actors, described as "a reference type that protects access to its mutable state, and is introduced with the keyword actor." Lattner said: "Because they are embodied by an (internal) queue abstraction, you communicate with actors asynchronously, and actors guarantee that the data they protect is only touched by the code running on that queue. This provides an 'island of serialization in a sea of concurrency'."

Another big feature in Swift 5.5 is package collections. As the name implies, a package collection is a group of packages, where a package is a set of Swift source code files and a manifest. "We envision educators and community influencers publishing package collections to go along with course materials or blog posts, removing the friction of using packages for the first time and the cognitive overload of deciding which packages are useful for a particular task," the original proposal for this feature stated.

Package collections are supported by the independent Swift Package Index and integrated into Xcode 13, the new version that Apple has released to tie in with its iOS 15 release and iPhone 13.

Swift 5.5 is available now as part of Xcode 13, and can also be downloaded for Ubuntu, CentOS, and Amazon Linux 2. A Windows 10 version is "coming soon."

Swift is Apple's language, introduced in 2014, made open source in 2015, and extended to Windows in 2020 (the toolchain also runs on Linux and, of course, macOS). Releases come roughly every six months, in March-April and September. It is a popular language though mostly for Apple platform development, ranking 11th in the Redmonk language rankings (where it has been since 2018), just ahead of in-house rival Objective-C, and 20th in the StackOverflow developer survey. It is a strongly-typed compiled language with features including null safety and automatic memory management.

Swift source code is on GitHub and in principle the enhancements in 5.5 will give the language a boost. That said, interest in server-side Swift has declined with IBM retreating at the end of 2019 despite early enthusiasm, and its strong association with Apple becomes a disadvantage when it comes to growing its use on other platforms. Swift programmers though will be glad to see the changes in 5.5. ®


Similar topics


Send us news

Other stories you might like