What’s new in Apple’s Swift 4.1 and 4.2 language
The programming language for MacOS and iOS apps offers an improved package manager, new source-code compatibility option, and many language improvements
Between the September 2017 release of Swift 4 and the planned fall 2018 release of Swift 5, Apple has released the intermediate Swift 4.1 version and is working on Swift 4.2.
Next version: Key features in Apple’s Swift 4.2
The planned Version 4.2 release of Apple’s Swift language is focused on stabilizing the language’s ABI (application binary interface). Swift 4.2 will serve as an intermediary step to achieving ABI stability in the planned Swift 5.0 release due late this year. ABI stability allows for binary compatibility between applications and libraries compiled with different Swift releases.
ABI changes in Swift 4.2, many of which tie into performance, are to be rolled out incrementally so there can be time for user feedback before changes are locked into place in the ABI.
Although Apple’s goal is stability, Swift 4.2 is not binary-compatible with previous releases. Source compatibility, however, is planned. Most source code built with the Swift 4.0 compiler should compile with the Swift 4.2 compiler and the Swift 4.1 compiler.
Other plans for Swift 4.2 include improvements to compile-time performance and numerous bug fixes.
The final branching for Swift 4.2, with changes merged into the master for the last time, is planned for April 20. Afterwards, a “bake” period will follow in which only some critical fixes will be put into the release.
Where to download snapshots of Swift 4.2
Swift 4.2 snapshots will be posted on Swift.org as part of integration testing.
Current version: What’s new in Swift 4.1
Released in lateMarch 2018, Swift 4.1 adds more generics to the language as well as build improvements. Version 4.1 is source-compatible with Swift 4.0.
Generics-related proposals implemented in the 4.1 release include conditional conformances, expressing the notion that a generic type will conform to a particular protocol only when type arguments meet specific requirements. This feature is expected to have a big impact on the Swift standard library. Other generics improvements include:
- Having the compiler synthesize
Hashableconformance, to reduce boilerplate and complexity.
- Making standard library index types
- Eliminating the associated type,
Collectionand modify uses to the concrete type,
Also featured in Swift 4.1 are more ways to configure builds, such as a code size optimization and easier mechanisms to target platform and environment support.
Under-the-hood changes in Version 4.1 were made as part of a plan to stabilize the ABI (application binary interace) in Swift 5, which is due late this year. These under-the-hood changes include use of a word-size field in the native object header for reference counting and audits of runtime functions. Also, various collection wrappers are being collapsed through the use of conditional conformance.
Swift 4.1 enhances the Swift Package Manager. It now resolves dependencies in package graphs that use URL schemes such as
http. Also, performance has been boosted for package graphs with shared dependencies.
The 4.1 upgrade also features Swift evolution proposals, including the removal of ownership keywords,
unowned, for property declarations in a protocol. This is being done for clarity. Also, cross-module struct initializers are being restricted. This matches an existing restriction for classes.
Where to download Swift 4.1
You can download the Swift 4.1 upgrade from the Swift download webpage. Also, Swift 4.1 is available with the Xcode 9.3 IDE, which features a code migrator to handle many source changes.
Previous version: What’s new in Swift 4.0
Swift 4.0 is a major upgrade to Apple’s Swift, the three-year old successor to the Objective-C language used for MacOS and iOS application development.
The Swift 4 upgrade enhances the Swift Package Manager and provides new compatibility modes for developers. Apple said Swift 4 also makes Swift more stable and improves its standard library. Swift 4 is largely source-compatible with Swift 3 and ships as part of Apple’s Xcode 9 IDE.
What’s new in Swift 4’s package manager
Swift Package Manager, which debuted in Swift 3, is tool for distributing code. It is integrated with the Swift build system to automate processes including downloading, compiling, and linking of dependencies. Improvements in Swift 4’s package manager include:
- A cleaner Package API that allows packages to specify new settings. These settings give developers more control over the building of packages and organization of sources on disk.
- Development of multiple packages in tandem has been made easier.
- Formalization of package products, enabling control over what libraries a package publishes to clients.
- Package builds on MacOS now occur in a sandbox, preventing network access and file-system modification, to reduce the reach of malicious manifests.
New compatibility modes in Swift 4 aid migration
Swift 4’s new compatibility modes could save you from having to modify code to be able to use the new version of the compiler. Two modes are supported, including the Swift 3.2 mode, which accepts most source files built with Swift 3.x compilers, and the Swift 4.0 mode, which includes Swift 4 and API changes.
Apple said that some source migration will be needed for many projects, but the number of source changes are “quite modest” compared to many previous major changes between Swift releases. The introduction of the compatibility modes in Swift 4 gives developers more control over the pace of migration.
Language improvements abound in Swift 4
Swift 4 introduces several language improvements, including:
- A faster and easier-to-use
Stringimplementation retaining Unicode correctness and supporting development and use of substrings.
- Improved use and management of Collection types.
- Enforcement of exclusive access to memory, in which potential modifications of variables must be exclusive with any other access to that variable.
- Ability to archive
- Type-safe serialization to external formats such as JSON.
- Protocol-oriented integers, which cleans up Integer APIs and making them more useful for generic programming.
- Support of
whereclauses to constrain associated types. Previously, associated types could express only simple inheritance constraints and not more sophisticated constraints available to generic types with the
- Inclusion of the
swift runcommand to build and run an executable defined in the current package.
- Removal of
finalkeyword support when declaring functions in protocol extensions.
- Improved interaction between
privatedeclarations and extensions, with
privateaccess control extended so members defined in an extension of a type have the same access as members defined on the type itself, as long as the type and extension are in the same source file.