Swift 5.1 Rollout: Significant Improvements You Need to Know
Swift 5.1: What’s Special with the New Features of Apple’s Xcode 11?
Swift 5.1 is Apple’s latest major release of its general-purpose, compiled programming language Swift. The new version offers greater stability along with several other improvements. If you have missed out on the new release features ever since Apple had announced the news on Swift.org, here is your best chance to take a look at the features that differentiate Swift 5.1 from its predecessors.
What’s New In Swift 5.1?
While Swift 5.0 was introduced with ABI stability that resolved Swift differences during runtime, Swift 5.1 comes with Module stability that reduces the differences during compile time. This means that future Swift releases can create and share binary frameworks. Additionally, critical language and standard library improvements along with Swift UI features are major changes.
Let us take a look into the details:
Module Format Stability
Swift 5.1 has attained Module Format Stability, that is, it can enable library compatibility at compile time. With this feature, third-party libraries can be used without worrying about the version of the Swift compiler with which it was built. To be more specific, if ABI stability ensures application compatibility during runtime, Module stability ensures library compatibility during compile time.
Module Format Stability can stabilize the module file to produce .swiftinterface, which is the compiler’s representation of the public interfaces in a framework. In a nutshell, with module format stability, a new text-based module interface file is defined and it describes the API of a binary framework.
New Language And Compiler Updates
1. Property Wrappers – Property Wrappers is the generalization of a user-accessible functionality utilized for defining custom-access patterns for atomic operations, property values, and so on.
The @propertyWrapper attribute has 4 major use cases such as constraining values, auditing property access, changing comparison semantics, and transforming values on property assignment.
2. Opaque Return Types – This type returns a value for a method, without essentially revealing the underlying type of value to the client that calls the API. This feature provides better encapsulation.
3. The return keyword is no more necessarily used for single expression functions as well as for getter functions.
4. Static subscripts and class subscripts are powerful features that can take arguments to change generic parameters to support many properties.
5. Synthesized memberwise initializers for structs – Unlike the earlier Swift versions where a memberwise initializer automatically gets created to accept parameters that matched the properties of a struct, here in Swift 5.1, the memberwise initializer now uses parameter values that are the default ones for any properties that use them.
6. Self is made universal to be able to refer to containing type while being used inside classes, enums, and structs.
7. The Optionals of Swift are implemented as enums of 2 different cases – some and none.
8. Optional enums can now be matched against non-optionals – While Swift already had ways to handle switch-case pattern matching between the optionals and non-optionals in case of integers and strings, Swift 5.1 has extended the feature to enums as well.
Standard Library Updates
- SIMD additions – Several API improvements such as a static scalarCount, .one, min, max, clamp, extending vectors, swizzling, etc. are a few major additions to Swift 5.1
- Array Initializer With Unlimited Access To Storage – Introducing a new initializer for arrays that need not pre-fill default values in the array. That is, with Swift 5.1, it is now possible to create an array of a particular size, without having to initialize every element.
- Ordered Collection Diffing – This new type represents the difference between collections, by which a line-by-line 3-way merge can be performed while coding.
- String Gaps and Missing APIs – Additional APIs for working with Strings such as creating and handling contiguous strings as well as missing functionality for users of String.
- Identifiable Protocol – For identifying distinct values that belong to a single logical entity.
Language Server Protocol (LSP)
The Language Server Protocol provides the reusability of language services. This feature is made possible across LSP-compliant editors and significantly reduces the effort that is required by the language team to make the language fully supported by various editors. This feature improves Swift’s usability which in turn improves the language adoption of the Apple ecosystem.
The SwiftSyntax update, for the libSyntax library, allows Swift tools to parse, generate, as well as transform the Swift source code. For instance, adding the SwiftPM dependency with a release tag does not require the Swift Compiler source and can be used in various scenarios. These scenarios include the following:
- Swift Pack – Used as a tool for the automatic embedding of Swift library source.
- Swift Rewriter – Used as a Swift code formatter.
- Periphery – Used to detect unused code.
- Muter – Used for mutation testing.
- Swift Stress Tester – Used as a test driver for sourcekitd-test.
Migration To Swift 5.1
Being source compatible with Swift 5.0, Swift 5.1 also supports compatibility modes for Swift 4.2 as well as for Swift 4/4.1. Xcode 11 of Apple consists of a code migrator that can take care of the source changes. A migration guide is also available that can help you through the various changes.
- Linux – Ubuntu 18.04, Ubuntu 16.04, and Ubuntu 14.04 have their official libraries available for download.
- Apple (Xcode) – Swift 5.1 ships as part of Xcode 11 to enable development on Apple’s platforms. A toolchain is also provided for download in Swift.org.
The Swift Programming Language has an updated version of Swift 5.1 on Swift.org as well as available for download on the Apple Books Store.
Swift 5.1: More On The Way!
Swift 5.1 is still under development and offers scope for more updates and changes in the coming days. Keeping in mind that the biggest feature was Module Stability, a beta of WWDC19 is still expected to come, the date of which cannot be decided at a fast pace.
To learn more about the recent advancements of Swift 5.1 and how the Swift team contributed to its massive development, stay tuned to our latest articles or drop us a call to get detailed guidance on the same.