Swift 5 has recently moved out of beta with the release of Xcode 10.2, including new language and standard library features, stricter memory exclusivity access guarantees, ABI stability, and more.
Most new syntax features added to Swift 5 do not break old source code. This has an exception for features that were removed because they were not intended in the first place and only worked due to some compiler glitches. An example of this is the removal of enumerations taking a variadic argument list, which will now trigger a compiler error:
// this will trigger an error in Swift 5:
enum X {
case foo(bar: Int...)
}
// replace with this:
enum X {
case foo(bar: [Int])
}
Swift 5 also introduces raw strings, which bring a new syntax to unclutter string literals containing double-quotes or backslashes by removing the need to escape them. For example, instead of writing:
let quote = "String may contain \"quotes\"."
let regex = "\\([^\\)]+\\)"
You can write:
let quote = #String may contain "quotes".#
let regex = #\([^\]+\)#
The only exception to the no-escape rule is string interpolation. In raw strings, you need to use the syntax \#(...)
, instead of \(...)
, because simple backslashes are interpreted literally and would not produce any interpolation.
At the standard library level, one of the most significant new features are SIMD types, which provide support for accelerated computation on small vectors and matrices. Standard library in SIMD types are an evolution of SIMD support provided by the simd framework. Old types, such as float2
, float3
, etc. are now aliases of the new standard library types so old code can compile without problems. The advantage brought by the new SIMD types is they are first-class citizens in Swift Standard Library, meaning they conform to a number of general protocols such as Hashable
, Equatable
, and Codable
, so you can easily integrate with other Standard Library types. Additionally, they provide a larger set of operators for vector-scalar arithmetic. All of this might require some changes in existing code to remove extensions that are not needed anymore, in case their functionality is already packaged within the Standard Library.
A number of other changes in Swift 5 may require fixes for existing code bases. In particular, you cannot override the hashValue
property for NSObject
and you should override the hash
property; the Sequence protocol does not define a SubSequence
subtype anymore and uses Array
for the same purpose.
InfoQ already covered several new Swift 5 features, including ABI stability, dynamically callable types, improved keypaths and try? operator, and extended string interpolation, when Swift 5 first become available in Xcode 10.2 beta. Additionally, the new Result type and memory exclusivity access were covered in two distinct articles.