InfoQ HomepageNewsSwift 6 Officially Available
Swift 6 Officially Available
Sep 26, 20243min read
Write for InfoQ
Feed your curiosity.Help 550k+ globalsenior developers
each month stay ahead.Get in touch
The Swift team has officially announced the availability of Swift 6, a new major version of Apple open-source language with focus on low-level and embedded programming, concurrent code safety, new cross-platforms APIs, and extended Linux and Windows support.
InfoQ has already covered several features brought by Swift 6, includingEmbedded Swift, a compilation mode that aims to address the specific constraints of embedded devices and kernel-level code;Swift Testing, a new cross-platform testing framework; and thedata-race free safe mode, aimed at helping developers create concurrent programs free of data races thanks to a new compile-time static detector.
Other major new features in Swift 6 are typed throws, memory ownership extensions for generics, 128 bit integers support, and extended C++ interoperability.
Typed throws enable the specification of the type of errors a function can throw as part of its signature. So, instead of declaring a generic throw clause,
func parseRecord(from string: String) throws -> Record { // ... }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 ado...try...catch block, the type of the error is already known at compile time:
func parseRecord(from string: String) throws(ParseError) -> Record { // ... }// call site:do { let record = try parseRecord(from: myString)} catch { // 'error' has type 'ParseError'}Interestingly, all Swift 6 functions have a typed throw signature under the hood. Indeed, a function with a non-typedthrows is equivalent to a function throwingAny Error, while a non-typethrows is equivalent tothrows(any Error).
It is important to stress thatthis 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 the authors of the evolution proposal "the existing (untyped) throws remains the better default error-handling mechanism for most Swift code".
The newmemory ownership model has been introduced in Swift 5.9 and Swift 6 brings it a tad further by extending support for generics "move only" types.
The concept of ownership is functional to controlling which piece of code is responsible for a given value to be eventually destroyed. Until Swift 5.9, Swift memory model was not exposed to programmers, as it was the case, e.g., with manual reference counting in Objective-C. Rather, it was encoded in a set of rules that worked pretty well in the general case, but made it harder to get more control on how values were destroyed using the default reference counting algorithm.
Without going into much detail, the memory ownership model exposed in Swift 5.9 is based on the concepts ofborrowing andconsuming, which allows customization of how initializers and functions treat ownership of the arguments they receive and, therefore, modify Swift default assumptions that initializers get the ownership (hence, consume) while functions do not (hence, they borrow).
An integral part of this model is represented by the"non copyable" protocol, which is used for "move only" types, i.e. a type whose values always have a unique ownership and cannot be copied. The implementation of the protocol in Swift 5.9 could not be used with generics, protocols, or existentials, butSwift 6 fills this gap by extending it. Covering the details of~Copyable for generics goes beyond the scope of the present article, so check out the linked Swift Evolution proposal for more details.
Related to the~Copyable protocol, Swift 6 leverages it to extend C++ interop to move-only types. If a C++ class has no copy constructor, Swift assumes it is~Copyable, with the possibility of explicitly ignoring an existing copy constructor using theSWIFT_NONCOPYABLE annotation. In addition, virtual methods, default arguments, and more standard library types such asstd::map andstd::optional are now supported.
Speaking of platform support, Swift 6 can be used now onmore Linux distributions, including Amazon Linux, Debian, Fedora, Red Hat, and Ubuntu, and Windows x86_64 and arm64 architectures. On Linux, theSwift 6 SDK supports building fully statically linked executables with no external dependencies and cross-compiling from other Linux platforms.
There is a lot more to Swift 6 than can be covered here, so do not miss the official announcement for the full details.
This content is in theMobile topic
Related Topics:
Related Editorial
Thriving in the Age of Agentic AI
Related Sponsors
Related Sponsor
%2ffilters%3ano_upscale()%2fsponsorship%2ftopic%2f56693164-d46e-497b-8f4c-f081ba291d9f%2fBoomiLogoRSB-1763407847346.png&f=jpg&w=240)
See Boomi AI Agents in action and unlock integration hyperproductivity responsibly. Embedded directly in the platform, Boomi AI Agents empower you to fast-track integrations, optimize workflows, and leverage agentic power across your digital landscape.Learn more.
The InfoQ Newsletter
A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers.View an example