What’s New in Swift 5.2
Swift 5.2 is now available as part of Xcode 11.4. In this article, you’ll get an overview of the changes you’ll see moving to Swift 5.2. By Bill Morefield.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
What’s New in Swift 5.2
20 mins
- Improved Diagnostics and Error Messages
- Easier Troubleshooting
- Not Just SwiftUI
- Syntactic Sugar Additions
- Calling Types as Functions
- Cleaner Syntax
- Machine Learning Application
- Key Path Expressions as Functions
- Subscripts With Default Arguments
- Major Bug Fixes
- Lazy Filters are Called in Order
- Default Values From Outer Scopes
- Warning When Passing Dangling Pointers
- Overridden Methods Can’t Use Incorrect Generics
- Class-Constrained Protocol Extensions
- Disambiguate Functions with Named Parameters
- Where to Go From Here
Disambiguate Functions with Named Parameters
You can now use the as
operator to disambiguate a call to a function with argument labels. Before, you could only do this for functions without label arguments. Consider these two functions:
func print(x: Int) { print("Int \(x)") }
func print(x: UInt) { print("UInt \(x)") }
Now you can differentiate the two functions using as
with the following syntax:
(print as (Int) -> Void)(5) // Prints Int 5
(print as (UInt) -> Void)(5) // Prints UInt 5
There is one side effect to this change: You can no longer use a generic typealias
to preserve the argument labels of a function reference using the as
operator. An example of this would be:
typealias Magic<T> = T
(print as Magic)(x: 5)
This code will now result in a compiler error:
Extraneous argument label 'x:' in call
Instead, you must eliminate the argument from the call:
(print as Magic)(5)
This prints Int 5 like the first call above.
Where to Go From Here
If you haven’t already, click the Download Materials button at the top or bottom of this article. Open these Playground files to explore many of the new changes and features for Swift 5.2.
While Swift 5.2 is not a major update, it does bring welcome changes and additions to the language. Nearly all developers will benefit from the improvements in diagnostics and error messaging. If they use machine learning in their projects, they’ll also appreciate the new type and key path features. And developers working with custom collections will welcome the addition of default subscript types.
If you would like to learn more about the Swift Evolution process, check out these links:
- GitHub change log for Swift. This document gives a short summary of the changes for each release.
- GitHub listing of Swift Evolution proposals. This document provides links to the GitHub page for each proposal. Use this to read the full description of the reasons and thought processes behind a given change.
- Swift.org bugs and issues dashboard. Use this site to view the full description, discussion and activity for the reported bugs and issues in Swift.
What are your thoughts about the new changes and features in Swift 5.2? Let us know in the forum discussion below!