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.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

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.

Note: For more information about this bug fix, see this article at Swift.org: SR-11429: Don’t look through CoerceExprs in markDirectCallee

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:

What are your thoughts about the new changes and features in Swift 5.2? Let us know in the forum discussion below!