The Swift team has officially announced the availability of Swift 6, a new major version of Apple open-source language focused on low-level and embedded programming, concurrent code safety, new cross-platform APIs, and expanded Linux and Windows support.
InfoQ has highlighted several new additions in Swift 6, including Embedded Swift, a compilation approach tailored for embedded devices and kernel-level coding; Swift Testing, a novel cross-platform testing framework; and a data-race free safety mode designed to assist developers in creating concurrent programs without data races using a new compile-time static detector.
Additional significant advancements in Swift 6 include typed throws, enhancements in memory ownership for generics, support for 128-bit integers, and improved C++ interoperability.
Typed throws facilitate specifying the type of errors a function can throw as part of their signature, moving away from a generic throw clause.
you can now explicitly declare the error type. This will bring the additional simplification that when you wrap the call to such a function in a do...try...catch
block, the type of the error is already known at compile time:
Interestingly, all Swift 6 functions have a typed throw signature under the hood. Indeed, a function with a non-typed throws
is equivalent to a function throwing Any Error
, while a non-type throws
is equivalent to throws(any Error)
.
It is important to stress that this feature is not meant to replace non-typed throws everywhere.
This feature is useful in generic code that forwards along errors thrown in client code, or in resource-constrained environments that cannot allocate memory, such as in embedded Swift code.
In fact, according to the authors of the evolution proposal, the traditional untyped throws remain a superior default method for error handling in most Swift programming scenarios.
A new memory ownership model was implemented in Swift 5.9, and Swift 6 extends this further by enhancing support for generics with “move only” types.
The principle of ownership is crucial for determining which section of code has the responsibility to eventually dispose of a value. Prior to Swift 5.9, the Swift memory model was largely abstract to developers, unlike manual reference counting in Objective-C, which was more explicit. Instead, it functioned under a set of rules that generally served well but did not allow much flexibility in managing how values were destroyed under the standard reference counting system.
Without delving deeply, the memory ownership paradigm introduced in Swift 5.9 revolves around the ideas of borrowing
and consuming
. This enables a customization of how initializers and functions handle the ownership of the arguments they accept, altering the usual assumptions of Swift where initializers take ownership (thus, consume) and functions do not (thus, they borrow).
An integral part of this model is represented by the “non copyable” protocol, which is utilized for “move only” types, that is, a type whose values are uniquely owned and cannot be duplicated. Although Swift 5.9’s implementation of this protocol did not support use with generics, protocols, or existentials, Swift 6 remedies this by extending the protocol’s capabilities. Exploring the intricacies of ~Copyable
for generics is outside the scope of this article, so for more comprehensive information, refer to the Swift Evolution proposal provided.
In relation to the ~Copyable
protocol, Swift 6 takes advantage of this feature to enhance C++ interoperability with move-only types. If a C++ class lacks a copy constructor, Swift treats it as ~Copyable
. This classification can also explicitly neglect an available copy constructor via the SWIFT_NONCOPYABLE
marker. Furthermore, support has been extended to virtual methods, default arguments, and additional standard library entities such as std::map
and std::optional
.
Regarding platform support, Swift 6 has expanded its reach and is now available on additional Linux distributions such as Amazon Linux, Debian, Fedora, Red Hat, and Ubuntu, as well as on Windows x86_64 and arm64 architectures. Specifically, on Linux, the Swift 6 SDK permits the creation of fully statically linked executables devoid of external dependencies and supports cross-compiling across different Linux platforms.
Swift 6 encompasses numerous features that are too extensive to discuss in this brief overview, so be sure not to miss the official release notes for all the details.
Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.