Swift 6.1, included in Xcode 16.3, introduces several improvements to the language and the Swift Package Manager, including type-wide global actor inference control, support for trailing comma in lists, package traits for conditionally exposing features based on the platform, and enhancements to Swift Testing.
Prior to Swift 6.1, the nonisolated
keyword could be used to indicate that a property or function was safe to call from any concurrent context. In Swift 6.1, this support extends to types and extensions. There are two main use cases: when working with an actor-isolated protocol that includes methods which don’t require isolation, or when opting out of actor isolation in a protocol for which isolation is globally inferred.
For the first case, consider the following code:
@MainActor
protocol IsolatedStruct {
// mutable state requiring isolation
}
extension IsolatedStruct: CustomStringConvertible, Equatable {
// properties and methods not requiring isolation
var description: String {
...
}
static func ==(lhs: S, rhs: S) -> Bool {
...
}
}
The methods in the extension do not need to be actor-isolated, still the Swift 6 compiler still requires them to run on @MainActor
unless they are explicitly marked nonisolated
. In Swift 6.1, you can now write nonisolated extension IsolatedStruct:...
to achieve the same effect.
For the second case, without diving into the complex rules governing global actor inference, it’s enough to say that if you want to override the compiler’s automatic decision about which global actor a type is assigned to, perhaps due to a property wrapper or another hard-to-trace dependency, you can mark the entire type as nonisolated
.
A minor but welcome change to the language syntax is the ability to use trailing commas in tuples, parameter and argument lists, generic parameter lists, closure capture lists, and string interpolations, in addition to the previously supported collection literals. This will be especially useful in plugins and macros, as generating comma-separated lists no longer requires a special case for the final element.
Package traits are a new Swift Package Manager feature used to define a set of traits a package offers. This makes it possible to express conditional compilation and optional dependencies, making it easier to offer different APIs and features when used in specific environments, such as Embedded Swift and WebAssembly.
Speaking of Swift Testing, the new TestScoping
protocol let you define custom test traits that modify the scope in which a test execute. This can be useful, for example, to bind a local value to a mocked resource. Moreovoer, Swift Testing now provides updated versions of the #expect(throws:)
and #require(throws:)
macros which return the error they caught.
Swift 6.1 includes many more improvements than we can cover here, so be sure to check out the official Swift evolution documentation for the full details.
As mentioned, Swift 6.1 is included in Xcode 16.3. It can also be installed on Linux using swiftly and on Windows using WinGet or Scoop.