Five years after Swift 5, version 6.0 of Apple’s open source programming language was released. The release mainly completes the work in preparation for the final point release in the area of ​​concurrent programming.
Advertisement
There are also extensions to the ownership concept, exception handling, and interaction with C++. Some convenient functions complete the linguistic innovations.
Extensive connection to “foreign” operating systems
In addition to linguistic innovations, there are additional features for platforms beyond macOS and iOS. The Foundation framework with basic data types, collections and functions for direct connection to the respective operating systems is now available for Linux and Windows as well as Apple’s operating systems.
There are also new installation packages for Linux without external dependencies, and ready-made builds for additional distributions. On Windows, the package manager parallelizes build operations on multi-core processors, and there is a kind of Windows-ready toolchain on ARM systems.
A preview of Embedded Swift will also be released alongside Swift 6. This is a subset of the language specifically designed for developing applications for microcontrollers. The toolchain initially targets ARM and RISC-V systems
Embedded Swift strips down functionality to reduce memory requirements for microcontroller applications.
(Picture: swift.org on GitHub,
Data race stopped
Improved concurrency is one of the key features of Swift 6.0. The switch to a new concurrency model has been ongoing since version 5.4 was released in early 2021. Structured concurrency was added in Swift 5.5.

Swift 6 aims to prevent data races, which can occur when multiple threads have write and read access to the same data at the same time, which can lead to unexpected errors. The Swift compiler checks whether this risk exists when exchanging data between threads. The compiler flag was already present in Swift 5.10 -strict-concurrency=complete
Which checked for data races when compiling, but gave many false positives. Compiler checks in Swift 6 are more reliable.
The basis for thread-safe data processing in Swift 6 Sendable
, The protocol is for exchange of values ​​between the threads. on Sendable
-Values ​​must ensure that they do not have shared mutable content, either through immutable content or locks that ensure that only one actor has access to mutable values ​​at any given time.
In There is a section on migrating to Swift 6 About how the code runs the data Sendable
Types and other methods.
Clear Owner
In the fall of 2023, Swift 5.9 introduced an ownership concept to the programming language that is not as strict as the original ownership model in Rust. The stated goal for Swift was, above all, to improve memory management. This includes the syntax ~Copyable
For objects that cannot be copied, i.e. they have a unique owner.
swift6 now leads as a counterpart Protocol Coppyable
The one that is the default for all values ​​other than ~Copyable
Are marked.
The language now allows functions that have both copyable and non-copyable values, as the following example from a Swift 6 blog post shows:
protocol Drinkable: ~Copyable {
consuming func use()
}
struct Coffee: Drinkable, ~Copyable { /* ... */ }
struct Water: Drinkable { /* ... */ }
func drink(item: consuming some Drinkable & ~Copyable) {
item.use()
}
drink(item: Coffee())
drink(item: Water())
Through awards ~Copyable
are in the protocol Drinkable
Values ​​that cannot be copied are also (but not only) allowed. So call the function drink
Both for non-copyable structure Coffee
as well as for copyable structure Water
allowed.
According to the Swift Blog, non-copyable types have already found their way into Swift’s standard library, including Atomic
-Type in the synchronization framework.
Exact error message
Another novelty in Swift 6 is typed throws: the type of a possible error message can now be specified using a parameter throws
prompt:
func parseRecord(from string: String) throws(ParseError) -> Record {
// ...
}
do {
let record = try parseRecord(from: myString)
} catch {
// 'error' has type 'ParseError'
}
Because of the type of possible error expected in the function, the calling function can assume that when catch
-blocks this error properly. The main purpose of the change is to save resources, for example for embedded applications.
Typed throws are also allowed in normal functions:
extension Sequence {
func map(_ body: (Element) throws(E) -> T) throws(E) -> (T) {
// ...
}
}
However, only one type of error can be specified, throws(FileError, ParseError)
Therefore not allowed. For cases with more than one possible type of error, the general rule still applies throws
for any error messages.
More feature and test libraries
In addition to the major innovations, Swift 6 also brings a lot of small things. The method is worth telling count(where:)
which is a combination filter()
and a counting function and eliminates unnecessary copying of array elements:
let scores = (10, 7, 5, 9, 3, 8, 6)
let goodCount = scores.count { $0 >= 7 }
New pack iteration Performs an iteration over the parameter packs introduced in Swift 5.9 — a flexible number of parameters — with a for
,in
syntax.
In addition, the current version of the programming language supports signed and unsigned 128-bit integer types.
Swift 6 also introduces a new library, Swift Testing, which provides an API for writing and managing tests. Also There is a separate project on GitHub,
More information about Swift 6 can be found Blog post with announcement Extracting the installation files for the programming language are on the download page Available for macOS, Linux, and Windows.
(RME)
