AWESOME: Test Collection

What’s new in Apple’s Swift 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

What’s new in Apple’s Swift 4 language
Stefan Berndtsson (CC BY 2.0)

Ahead of the planned 2019 release of Swift 5, Apple has released the intermediate Swift 4.2 version.

Current version: What’s new in Apple’s Swift 4.2

Version 4.2 of Apple’s Swift programming language brings the language closer to a stabilized application binary interface (ABI) and enables binary compatibility for future releases. ABI stability enables binary compatibility between libraries and applications compiled with different versions of the language.

Although Apple’s goal is stability, Swift 4.2 is not binary-compatible with previous releases. Source compatibility, however, is provided. Most source code built with the Swift 4.0 compiler should compile with both the Swift 4.2 and Swift 4.1 compilers.

Other new features in Swift 4.2 are:

  • Generics improvements, anchored by the completion of conditional conformance support, reducing the amount of boilerplate code and making more code reusable.
  • Standard library features include improvements to the Hashable protocol and a unified set of randomization functions and protocols.
  • Batch-mode compilation support, which improves build times.
  • A change in the calling convention for the retain/release cycle, to reduce code size and improve performance.
  • Recursive metadata support.
  • More-compact reflection metadata.
  • Deflection arguments inlined at call sites.
  • Various bug fixes, such as for inheritance of generic initializers.
  • The compiler directive that is syntactically equivalent to the #if swift version check but checks against the version of the compiler, regardless of which compatibility mode is running.
  • New Swift package manager capabilities, including support for batch mode, with targets now compiled using this mode; improved scheme generation logic; and automatic Xcode project generation.

Where to download Swift 4.2

You can download Swift 4.2 by downloading the Xcode 10.0 IDE from Apple.

Other plans for Swift 4.2 include:

  • Improvements to compile-time performance.
  • Strings are now 16 bytes instead of 24 bytes. Apple sees this as a good trade-off between memory usage and performance, while being large enough to do small string optimizations.
  • APIs are offered for easier random-number generation.
  • The has import build configuration directive better expresses intent when sharing code between iOS and MacOS.

Previous version: What’s new in Swift 4.1

Released in late March 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 Equatable and Hashable conformance, to reduce boilerplate and complexity.
  • Making standard library index types Hashable.
  • Eliminating the associated type, IndexDistance, from Collection and modify uses to the concrete type, Int, instead.

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 ssh and 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, weak and 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 String implementation 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 struct and enum types.
  • 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 where clauses to constrain associated types. Previously, associated types could express only simple inheritance constraints and not more sophisticated constraints available to generic types with the where clause.
  • Inclusion of the swift run command to build and run an executable defined in the current package.
  • Removal of final keyword support when declaring functions in protocol extensions.
  • Improved interaction between private declarations and extensions, with private access 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.

Copyright © 2018 IDG Communications, Inc.