Apple releases Swift 5.5 with async keyword • The Register

0


Apple released version 5.5 of the Swift programming language, described as a “massive release”, comprising async / wait keywords, package collections, and improved Objective-C interoperability.

The most anticipated new features in Swift 5.5 are concurrency.

One of the problems with Swift’s earlier approach to concurrency was that when multiple asynchronous operations were written by a developer, the result would be a “pyramid of doom” – nested calls that were difficult to track and made it difficult to control. flow and handling of problematic errors. “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 / wait model as it appears in Microsoft’s C # (inspired by asynchronous workflows in F #) and adopted by other languages, notably 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
}

Additionally, anonymous closures are implicitly async if they contain a wait expression.

This new competitive model will also improve interoperability with Objective-C, Apple’s old language. Objective-C has completion handler methods which will be translated to asynchronous methods in Swift, and asynchronous Swift methods can also be exported as completion handler methods.

A second key element of the Swift concurrency model is that of actors, described as “a type of reference that protects access to its modifiable state and is introduced with the keyword actor”. Lattner said, “Because they’re embodied by an (internal) queue abstraction, you communicate with actors asynchronously, and actors ensure that the data they’re protecting is only affected by code. run on this queue. This provides an ‘island of serialization in a sea of ​​competition’.

Another great feature of Swift 5.5 is package collection. As the name suggests, a package collection is a group of packages, where a package is a collection of Swift source code files and a manifest. “We envision educators and community influencers to publish collections of packages to accompany course materials or blog posts, thereby eliminating the friction of using the 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 with Xcode 13, the new version released by Apple to partner with its iOS 15 version 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 works on Linux and, of course, macOS). The outings take place approximately every six months, in March-April and September. It’s a popular language, but primarily for the development of the Apple platform, ranking 11th in the Redmonk language rankings (where it has been since 2018), just ahead of its internal rival Objective-C and 20th in the StackOverflow developer survey. It is a strongly typed compiled language with features such as zero security and automatic memory management.

The source code for Swift is on GitHub and, in principle, the improvements in 5.5 will give the language a boost. That said, interest in server-side Swift waned with IBM’s hindsight in late 2019 despite early enthusiasm, and its strong association with Apple becomes a drawback when it comes to expanding its use on other platforms. shapes. Swift programmers will be happy to see the changes in version 5.5, however. ®


Share.

Leave A Reply