Is 2025 the 12 months to completely undertake Swift 6? – Donny Wals

Is 2025 the 12 months to completely undertake Swift 6? – Donny Wals


When Apple launched Xcode 16 final 12 months, they made the Swift 6 compiler accessible together with it. Because of this we will create new initiatives utilizing Swift 6 and its compile-time knowledge race protections.

Nevertheless, the massive query for a lot of builders is: Is 2025 the best time to undertake Swift 6 absolutely, or ought to we follow Swift 5 for now?

On this submit, I gained’t offer you a definitive reply. As a substitute, I’ll share my perspective and reasoning that can assist you determine whether or not adopting Swift 6 is best for you and your undertaking(s).

The precise reply relies on a great deal of variables just like the undertaking you’re employed on, the group you’re employed with, and your information of Swift Concurrency.

Xcode 16, present initiatives, and Swift 6

For those who’ve opened an present undertaking in Xcode 16, you won’t have observed any quick adjustments. Whereas the Swift 6 compiler is utilized in Xcode 16 for all initiatives, Xcode defaults to the Swift 5 language mode for present initiatives.

For those who’ve skilled earlier main migrations in Swift, you’ll keep in mind that Xcode would often immediate you to make adjustments to your undertaking as a way to be sure your undertaking nonetheless works. This occurred for the migration from Swift 1.2 to Swift 2, and from Swift 2 to Swift 3.

We bought a brand new compiler, and we had been compelled to undertake the brand new Swift language model that got here together with it.

Since then, the compiler has gained some “language modes”, and the Swift 6 compiler comes with a Swift 5 language mode.

The Swift 5 language mode permits the Swift 6 compiler to perform with out imposing all of the stricter guidelines of Swift 6. For instance, the Swift 5 language mode will make it in order that compile-time knowledge race protections will not be turned on.

So, once we speak about adopting Swift 6, we’re actually speaking about opting into the Swift 6 language mode.

Present initiatives which can be opened in Xcode 16 will, routinely, use the Swift 5 language mode. That’s why your undertaking nonetheless compiles completely superb with out adopting Swift 6.

What about new initiatives?

New initiatives in Xcode 16 additionally default to Swift 5 language mode. Nevertheless, Swift packages created with the Swift 6 toolchain default to Swift 6 language mode except explicitly configured in any other case. This distinction is essential, as a result of whenever you create new packages you’re working in a special language mode than undertaking (and that’s completely superb).

For those who’re serious about enabling the Swift 6 language mode for present initiatives or packages, I’ve some weblog posts about that right here:

Challenges of Adopting Swift 6

Switching to Swift 6 language mode could make initiatives that compiled simply superb with Swift 5 break utterly. For instance, you’ll run into errors about capturing non-sendable parameters, sendable closures, and actor isolation.

Some fixes are easy—like making an immutable object explicitly sendable or refactoring objects which can be utilized in async features into actors. Nevertheless, different points, particularly these involving crossing isolation boundaries, might be a lot trickier to repair.

For instance, including actors to resolve sendability errors typically requires refactoring synchronous code into asynchronous code, resulting in a ripple impact all through your codebase. Even seemingly easy interactions with an actor require await, even for non-async features as a result of actors function in their very own isolation contexts.

Adopting actors is usually a job that may take a lot, for much longer than you would possibly anticipate initially.

Resolving errors with @MainActor

A typical workaround is to liberally apply @MainActor annotations. Whereas this reduces concurrency-related errors by forcing most code to run on the primary thread, it’s not all the time the answer that you just’re on the lookout for. Whereas not inherently mistaken, this method must be used with warning.

Decreasing crossing of isolation boundaries

Apple acknowledges the challenges of adopting Swift 6, particularly for present initiatives. One important facet of Swift Concurrency that may make adoption difficult is how non-isolated asynchronous features inherit isolation contexts. At the moment, nonisolated async features run on a background thread except explicitly remoted, which might result in pointless crossing of isolation boundaries.

Apple is exploring methods for such features to inherit the caller’s isolation context, probably decreasing sendability errors and making adoption of Swift 6 rather more easy.

So, ought to we undertake Swift 6?

For present initiatives, I like to recommend continuing cautiously. Stick to Swift 5 language mode except:

• Your undertaking is small and manageable for migration.

• You’ve got a robust understanding of concurrency ideas and may decide to resolving sendability points.

New initiatives might be constructed with Swift 6 language mode from the beginning, however be ready for challenges, particularly when interacting with Apple’s frameworks, which can lack full concurrency help.

For those who’re modularizing your codebase with Swift packages, I like to recommend utilizing Swift 6 language mode in your (new) packages, as packages usually have fewer dependencies on Apple’s frameworks and are simpler to adapt and you’ll have Swift 5 and Swift 6 modules in the identical undertaking.

On the brink of undertake Swift 6

Earlier than adopting Swift 6, make sure you perceive:

• Sendability and how you can resolve associated errors.

• The usage of actors and their influence on isolation and asynchronicity.

• How one can navigate ambiguous compiler errors.

I cowl all of those matters and extra in my ebook, Sensible Swift Concurrency in addition to my workshops. You may as well assessment and research Swift evolution proposals and discussion board discussions to get a superb sense of how Swift Concurrency works.

For those who’ve began adopting Swift 6 or determined to carry off, I’d love to listen to your experiences! Join with me on X, BlueSky, or Mastodon.

Leave a Reply

Your email address will not be published. Required fields are marked *