The Swift 5.8 Updates That Are Exciting Developers

2

While Swift Evolution is currently working on several significant changes to the Swift programming language, Swift 5.8 is primarily a release focused on refining and improving existing functionality. This release includes several exciting upgrades, with an emphasis on improving and cleaning up features that are already widely used.

 

2

 

Ability to Lift All Limitations on Variables in Result Builders

The latest update to result builders in Swift has eased some of the limitations imposed on variables, enabling developers to write code that was previously considered invalid by the compiler.

In addition, this modification grants users the ability to employ local computed properties and property wrappers within result builders. However, it’s worth noting that their usefulness in this context may be limited.

 

New Function: Back Deployment

Swift 5.8 introduces a new attribute called “@backDeployed,” which enables developers to use new APIs on older framework versions. This is accomplished by embedding the function’s code into the app’s binary and performing a runtime check. If the user’s operating system is recent enough, the system’s version of the function will be utilized. Otherwise, the function’s copy from the app’s binary will be used.

The @backDeployed attribute can only be used with functions, methods, subscripts, and computed properties. As a result, it may be effective for minor API modifications, such as the fontDesign() modifier introduced in iOS 16.1. However, it is not suitable for changes that involve the use of new types, such as the scrollBounceBehavior() modifier, which relies on a new ScrollBounceBehavior structure.

 

4

 

Implicit Self for Weak Self Captures, After Self is Unwrapped

In previous versions of Swift, developers have had to include the “self” keyword when referencing properties or methods within closures that capture self. This is because the closure holds a strong reference to the object, which can lead to retain cycles and memory leaks.

However, the latest update to Swift (Swift 5.8) takes us closer to removing the need for “self” in closures. In particular, it introduces a new feature that allows for implicit self in cases where a weak self capture has been unwrapped. This means that developers can now access properties and methods of the enclosing instance within a closure without explicitly referencing “self,” as long as the capture is declared as “weak” and unwrapped before use.

This is a major improvement for Swift developers, as it simplifies the syntax and makes code easier to read and write. Additionally, it helps to prevent common memory management issues that can arise when working with closures that capture self.

 

Concise Magic File Names

The latest Swift 5.8 update modifies the #file magic identifier to follow the format Module/Filename, such as MyApp/ContentView.swift. 

Previously, the #file identifier contained the complete path to the Swift file, such as /Users/twostraws/Desktop/WhatsNewInSwift/WhatsNewInSwift/ContentView.swift, which was overly verbose and made the code harder to read.

The Swift evolution proposal for these changes states that the update will improve the binary size and execution performance, leading to further enhancements in Swift 6. 

 

33

 

Open Existential Arguments to Optional Parameters

Swift 5.8 expands upon a feature introduced in Swift 5.7, which allows developers to call generic functions using a protocol. This update addresses a small but frustrating inconsistency in the previous version of Swift: while Swift 5.7 permitted calling generic functions with protocols, it did not allow this behavior with optionals.

The new update in Swift 5.8 addresses this inconsistency by extending the protocol-based approach to optional types. This will allow developers to use protocols to call generic functions that accept optional types, making the language more consistent and user-friendly.

This update is exciting for developers because optionals are a fundamental concept in Swift, allowing developers to handle situations where a value may be missing. By extending the protocol-based approach to optionals, Swift developers can now use the same generic functions with optional types as they do with non-optional types, reducing the need for workarounds or additional code.

 

New Support for Collection Downcasts in Cast Patterns

The latest update in Swift addresses a small but potentially frustrating inconsistency that could occur when downcasting collections. Specifically, in certain circumstances, Swift would not allow developers to downcast a collection, such as casting an array of objects belonging to ClassA to an array of objects belonging to a different class that inherits from ClassA.

This update in Swift resolves this inconsistency and makes it possible for developers to perform downcasting on collections in all scenarios. This means that developers can now more easily work with collections of objects that belong to a specific class or its subclasses, improving the flexibility and functionality of their code.

 

A New Era of Swift

Swift 5.8 introduces a range of updates and improvements that build on previous releases and enhance the language’s functionality and flexibility. From improving result builders to addressing inconsistencies in downcasting collections, these updates are designed to make the development process smoother and more intuitive. 

With these updates, Swift continues to demonstrate its commitment to innovation and user-centered design, providing developers with the tools they need to create powerful, efficient, and user-friendly applications.

Subscribe to our newsletter!