Swift 6 programming language focuses on concurrency, Linux and Windows

In addition to language enhancements, the programming language brings improvements for connecting platforms beyond Apple's ecosystem.

listen Print view

(Image: Erstellt mit Dall-E von iX-Redaktion)

6 min. read
Contents

Five years after Swift 5, Apple's open-source programming language has been released in version 6.0. The release primarily completes the preparatory work of the last point releases in the area of concurrent programming.

There are also enhancements to the ownership concept, exception handling and interaction with C++. Some comfort functions round off the linguistic innovations.

Videos by heise

In addition to the linguistic innovations, there are additions for platforms beyond macOS and iOS. The Foundation framework with basic data types, collections and functions for direct connection to the respective operating system is now available for Linux and Windows as well as for Apple's operating systems.

For Linux, there are also new installation packages without external dependencies and ready-made builds for additional distributions. Under Windows, the package manager parallelizes build processes on multi-core processors, and there is a variant of the toolchain that is tailored for Windows on ARM systems.

Swift 6 is also a preview of Embedded Swift. This is a subset of the language that is specifically tailored to the development of applications for microcontrollers. The toolchain is initially aimed at ARM and RISC-V systems

Embedded Swift reduces functionality to minimize memory requirements for microcontroller applications.

(Image: Swift.org auf GitHub)

Improved concurrency is one of the most important features of Swift 6.0. The switch to a new concurrency model has been underway since version 5.4, which was released at the beginning of 2021. Structured concurrency was added in Swift 5.5.

Swift 6 is designed to prevent data races that can occur when multiple threads have read and write access to the same data at the same time, which can lead to unexpected errors. The Swift compiler checks whether this risk exists when data is exchanged between threads. Swift 5.10 already had the compiler flag -strict-concurrency=complete, which checked for data races during compilation, but generated many false positives. In Swift 6, the compiler checks are much more reliable.

The basis for thread-safe data processing in Swift 6 is Sendable. The protocol is designed for the exchange of values between threads. For Sendable values, care must be taken to 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.

The Swift 6 migration guide has a section on how to prevent code data races with Sendable types and other methods.

In fall 2023, Swift 5.9 introduced an ownership concept for the programming language that is not as strict as the basic ownership model in Rust. The declared goal for Swift was primarily to improve memory management. This includes the syntax ~Copyable for objects that cannot be copied, i.e. have a unique owner.

Swift 6 now introduces the Coppyable protocol as a counterpart, which applies by default to all values that are not marked as ~Copyable.

The language now allows functions in which both copyable and non-copyable values are permitted, as the following example from the 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())

By marking ~Copyable, values that are not copyable are also (but not only) allowed in the Drinkable protocol. Therefore, calling the function drink is permitted for both the non-copyable struct Coffee and the copyable struct Water.

According to the Swift blog, the non-copyable types have already found their way into the Swift standard library, including the Atomic type in the synchronization framework.

Typed Throws are another new feature in Swift 6: The type of possible error message can now be specified via a parameter for throws:

func parseRecord(from string: String) throws(ParseError) -> Record { 
  // ... 
}

do {
  let record = try parseRecord(from: myString)
} catch {
  // 'error' has type 'ParseError'
}

Based on the potentially expected error type specified in the function, the calling function can assume that exactly this error has occurred when the catch block is reached. The main aim of the change is to save resources, for example for embedded applications.

Typed throws are also permitted in generic functions:

extension Sequence {
  func map<T, E>(_ body: (Element) throws(E) -> T) throws(E) -> [T] { 
    // ... 
  }
}

However, only exactly one error type may be specified at any one time, so throws(FileError, ParseError) is not permitted. For cases with more than one potential type of error, the general throws still applies for any error messages.

In addition to the major new features, Swift 6 comes with numerous smaller additions. Worth mentioning is the count(where:) method, which offers a combination of filter() and a counting function and makes unnecessary copying of array elements superfluous:

let scores = [10, 7, 5, 9, 3, 8, 6]
let goodCount = scores.count { $0 >= 7 }

The new pack iteration introduces an iteration over the parameter packs – of a flexible number of parameters – introduced in Swift 5.9 with a for-in syntax.

The current version of the programming language also recognizes 128-bit signed and unsigned integer types.

Swift 6 also introduces Swift Testing, a new library that offers an API for writing and managing tests. There is a separate project for this on GitHub.

Further details on Swift 6 can be found in the blog post with the announcement. Installation files for the programming language are available on the download page for macOS, Linux and Windows.

(rme)

Don't miss any news – follow us on Facebook, LinkedIn or Mastodon.

This article was originally published in German. It was translated with technical assistance and editorially reviewed before publication.