What's New In Swift 5.0

Swift 5.0 is a major release of Swift and it brings ABI (Application Binary interface) stability and several major key new features including raw strings, future enum cases, a Result type, checking for integer multiples and more.

Introduction

 
Swift 5.0 introduced new capabilities that are the building blocks for future versions, including a re-implementation of String, enforcement of exclusive access to memory during runtime, new data types, and support for dynamically callable types. Swift 5.0 is available as a part of Xcode 10.2.
 

Motivation and goals

The primary goal of Swift 5.0 is for the language to achieve ABI (Application binary interface) stability. This will enable Swift runtime to be deployed by OS vendors that can be linked to the executables and libraries.

Related to ABI stability, module stability will be the primary focal point as well. This will land either in the Swift 5.0 release or in a subsequent one depending on its readiness.

Language Updates 

 
ABI Stability and Binary Compatability 

The ABI is now declared stable for Swift 5 on Apple platforms. As a result, Swift libraries are now incorporated into every macOS, iOS, tvOS, and watchOS release going forward. Your apps will be easier to build and smaller because they won’t have to include those libraries.

What about other platforms?

ABI stability is implemented for each operating system that it compiles and runs on. Swift’s ABI is currently declared stable for Swift 5.0 on Apple platforms. As the development of Swift on Linux, Windows, and other platforms matures, the core team of Swift will evaluate stabilizing the ABI on those platforms. 

The Standard Library updates in Swift 5.0 includes the following new features

  • String reimplementation with a UTF-8 encoding which can often speed up the code (See UTF-8 String blog post for more).
  • Improved support for raw text in string literals. 
  • Result and SIMD vector types added to the Standard Library
  • Enhancements to String interpolation, adding more flexibility to construct text from data
  • Performance improvements to Dictionary and Set

Swift 5.0 implements the following Standard Library proposals from the Swift Evolution process

  • Raw Strings
  • A standard Result type
  • Customizing String interpolation
  • Dynamically callable types
  • Handling future enum cases
  • Flattening nested optionals resulting from try?
  • Checking for integer multiples
  • Transforming and unwrapping dictionary values and compactMapValues()
  • Conform Never to Equatable and Hashable
  • Characters properties
  • Add Codable conformance to Range Types 
  • Make Numeric Refine a new AdditiveArithmetic Protocol 
  • Introduce withContiguous{Mutable}StorageIfAvailable methods 

Raw String 

The ability to create raw strings, where backslashes and quote marks are interpreted as those literals symbols rather than escape characters or string terminator. This makes a number of use cases more easy, and regular expressions in particular will benefit.

To use raw string, place one or more # symbols before your strings like this,
  1. import Foundation  
  2. let rain = #"The "rain" in "India" falls mainly from July to July end."#  
The # symbols at the start and end of the string become part of the string delimiter, so Swift understands that the standalone quote marks around “rain” and “India” should be treated as literal quote marks rather than ending the string.
 
Raw string backslash allows you to:
  1. import Foundation  
  2. let keypaths = #"Swift keypaths such as \Person.name hold uninvoked references to properties."#  

Additional Updated and Compiler Updates

 
Swift 5 defaults to enforcing exclusive access to memory for both debug and release builds (See the Swift 5 Exclusivity Enforcement blog post for more information about this update). And Swift 5 supports dynamically callable types that help improve interoperability with dynamic languages such as Python, JavaScript and Ruby. 

 
Package Manager Updates

 
Swift package manager includes a number of new features in Swift 5.0, including dependency mirroring, target-specific build settings, customized deployment targets, and the ability to generate code coverage data. Additionally, the Swift run command now includes the ability to import libraries in a REPL without needing to build an executable.
 
Swift 5 implements the following Package Manager proposals from the Swift Evolution process,

Migration to Swift 5.0

 
Swift 5.0 is source compatible with Swift 4, Swift 4.1 and Swift 4.2.
 
To help with moving to Swift 5 from earlier releases of Swift, Apple’s Xcode 10.2 contains a code migrator that can automatically handle many of the needed source changes. There is also a migration guide available to guide you through many of the changes.