Exploring the Power of Swift 5.8: How To Enable Upcoming Features?

Starting from Swift 5.8 you can adopt upcoming Swift features using a new compiler flag and compilation condition. 

Let see how you can use it in your projects.

Issue

Swift is open source so every time changes go from the proposal evaluation process and some of the changes are getting accepted by discussion.

Important part in all changes in Swift is source compatibility!

Sometime, a proposed change that breaks source compatibility is considered important enough to be accepted. It will not be accepted and implemented immediately but it will be released with next major version.

For one feature Apple has given a new compiler flag for specific feature in Xcode 14 which contains a feature of Swift 6, but adding number of separate compiler flags for every upcoming feature does not scale well.

General Solution

Instead of creating a different compiler flag for each upcoming feature, the compiler gains one new compiler flag called “enable upcoming feature” that is followed by the name of the feature to which need to be enabled:

Example:

In Swift Package Manager manifests you specify these using a new SwiftSetting:

Checking For Features from Code

Swift Language also introduced a new hasFeature() compilation condition that checks whether a feature is enabled. This allows you to write code that uses a feature if present or uses alternate code if not present.

Note: hasFeature() was introduced with the Swift 5.8 compiler.

How To Enable Upcoming Features

You can enable an upcoming feature by setting the compiler flag for a specific target. If you want to enable multiple features, use the compiler flag multiple times, as per number of enabled features.

In Xcode

For Xcode projects, add the compiler flags to the “Other Swift Flags” build setting:

  • In the project navigator, select the project
  • In the project editor, select the desired target or the project itself
  • Select the Build Settings tab
  • Make sure the All scope button is selected
  • Search for “swift flags” to find the Other Swift Flags build setting
  • Double-click to add one or more -enable-upcoming-feature flags to the setting

The screenshot below shows three upcoming features being enabled:

In SwiftPM Packages

For a Swift package, enable upcoming features for a target in its SwiftSetting array in the package manifest:

You will also need to update the tools version specified in the manifest to 5.8 or later:

Conclusion

There are two benefits to enabling upcoming features in your code.

First is you’ll find out immediately if your code will require any changes. It gives you the flexibility to update your code now or when it best fits your schedule.

Second is you can start using the upcoming features right away. You’ll also begin building experience with the upcoming syntax and behavior.

Let me know if you have any questions, comments, or feedback – via Twitter. You can follow me to build great Apple community.

Happy Coding 🙂