Swift 5.5 unleashed with async keyword to fix 'pyramid of doom', plus other changes in 'massive release'
- Reference: 1632288731
- News link: https://www.theregister.co.uk/2021/09/22/swift_5_5_released_with_async/
- Source link:
The most anticipated new features in [1]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 [2]put it .
[3]
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.
[4]
[5]
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 [6]original proposal for this feature stated.
[7]
Package collections are supported by the independent [8]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.
[9]Swift tailored for Windows no longer folklore: Apple's programming language available for Microsoft OS
[10]Mac OS X at 20: A rocky start, but it got the fundamentals right for a macOS future
[11]JavaScript, GitHub, AWS crowned winners in massive survey of 32,000 developers
Swift 5.5 is available now as part of Xcode 13, and can also be [12]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 [13]Redmonk language rankings (where it has been since 2018), just ahead of in-house rival Objective-C, and 20th in the StackOverflow [14]developer survey . It is a strongly-typed compiled language with features including null safety and automatic memory management.
Swift source code is [15]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 [16]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. ®
Get our [17]Tech Resources
[1] https://swift.org/blog/swift-5-5-released/
[2] https://gist.github.com/lattner/31ed37682ef1576b16bca1432ea9f782/
[3] https://pubads.g.doubleclick.net/gampad/jump?co=1&iu=/6978/reg_software/front&sz=300x50%7C300x100%7C300x250%7C300x251%7C300x252%7C300x600%7C300x601&tile=2&c=2YUr@40y3XNvHC33zfqHiBgAAANA&t=ct%3Dns%26unitnum%3D2%26raptor%3Dcondor%26pos%3Dtop%26test%3D0
[4] https://pubads.g.doubleclick.net/gampad/jump?co=1&iu=/6978/reg_software/front&sz=300x50%7C300x100%7C300x250%7C300x251%7C300x252%7C300x600%7C300x601&tile=4&c=44YUr@40y3XNvHC33zfqHiBgAAANA&t=ct%3Dns%26unitnum%3D4%26raptor%3Dfalcon%26pos%3Dmid%26test%3D0
[5] https://pubads.g.doubleclick.net/gampad/jump?co=1&iu=/6978/reg_software/front&sz=300x50%7C300x100%7C300x250%7C300x251%7C300x252%7C300x600%7C300x601&tile=3&c=33YUr@40y3XNvHC33zfqHiBgAAANA&t=ct%3Dns%26unitnum%3D3%26raptor%3Deagle%26pos%3Dmid%26test%3D0
[6] https://github.com/apple/swift-evolution/blob/main/proposals/0291-package-collections.md
[7] https://pubads.g.doubleclick.net/gampad/jump?co=1&iu=/6978/reg_software/front&sz=300x50%7C300x100%7C300x250%7C300x251%7C300x252%7C300x600%7C300x601&tile=4&c=44YUr@40y3XNvHC33zfqHiBgAAANA&t=ct%3Dns%26unitnum%3D4%26raptor%3Dfalcon%26pos%3Dmid%26test%3D0
[8] https://swiftpackageindex.com/
[9] https://www.theregister.com/2020/09/23/swift_windows/
[10] https://www.theregister.com/2021/03/26/mac_at_20/
[11] https://www.theregister.com/2021/07/16/jetbrains_developer_survey_2021/
[12] https://swift.org/download/
[13] https://redmonk.com/sogrady/2021/08/05/language-rankings-6-21/
[14] https://insights.stackoverflow.com/survey/2021#technology-most-popular-technologies
[15] https://github.com/apple/swift
[16] https://www.theregister.com/2019/12/17/swift_ibm_pulls_back_open_source_priorities/
[17] https://whitepapers.theregister.com/
Re: A sink
Every 'await' is essentially a manual yield back to the event loop, where the function stops running and other asynchronous code can be scheduled. It's basically co-operative threading and you are subject to the same kinds of concurrency bugs you would get in a regular multithreaded program with syncronous I/O. It would be better if the language simply provided green threads and made async I/O look like sync I/O, or just use threads.
await
That code is lovely but the first "await"ed call will need to finish before the second starts.
Is there a way of saying "Run all these, get back to me when they're all done" rather than "do this, then do that, then do that, then do that"?
Re: await
Fortunately, there is a simple way, described here:
https://docs.swift.org/swift-book/LanguageGuide/Concurrency.html#ID641
Re: await
Also in the 5.5 release is structured concurrency, I'm still reading it, but it seems to address what you ask : https://github.com/apple/swift-evolution/blob/main/proposals/0304-structured-concurrency.md
Re: await
> That code is lovely but the first "await"ed call will need to finish before the second starts.
What, really?
As a non-Swift developer I looked at that example and thought: cool, each await statement runs in parallel and the surrounding code automatically waits at the end of the enclosing block for all of them to finish.
As that is apparently not the case, I'm now asking myself why not, as that would be a lot more convenient?
re: 'island of serialization in a sea of concurrency'
Or "Single threaded apartment" as we called them in COM 30 years ago. Plus ca change.....
Not (currently) supported before iOS 15
The big problem with the Swift 5.5 release on Apple platforms is that there is no support for the concurrency changes on OS versions before iOS 15 / Mac OS 12, which, despite the rapid upgrade of most Apple users, still leaves those real world developers whose apps support older OS versions out in the cold. It's fine for hobbyist developers, but in the real world forcing a latest OS minimum is tough - even if 90%+ of active devices will be using it within 6 months, that last few % tend to be *very* vocal.
There are some comments from internal Apple people on the Swift forums that work is ongoing to back port the changes but no certainty that it will work, or how far back it would offer support. A lot of major third party systems still support back as far as iOS 10.0, so it will be years before they move to a minimum of 15.0 and make this usable, in the meantime they are pushing forward with a Swift 6.0 which sounds like it might also need more OS support and thus be iOS 16 as a base.
A sink
After working in a few programming languages and styles, I can confidently say that most asynchronous designs are antipatterns. There are few times when you call a method but don't need the answer until later. When most calls are synchronous by default, you're adding async wrappers 4% of the time. When they're async by default, you're adding unwrapping or chaining 96% of the time.