What's New In Swift 5.1

Swift 5.1 is the default version of Swift that's included in Xcode 11. You can use Xcode 11 to build targets that are written in either Swift 5.1, Swift 4.2, or Swift 4.
 
When you use Xcode 11 to build Swift 4 and Swift 4.2 code, most of the Swift 5.1 functionality is available. That said, the following changes are available only to code that uses Swift 5.1 or later.
  • Functions that return an opaque type require the Swift 5.1 runtime
  • The try? expression doesn't introduce an extra level of optionality to expressions that already return options.
  • Large integer literal initialization expressions are inferred to be of the correct integer type.

Motivation and Goals

 
The primary goal of Swift 5.1 is for the language to achieve module stability. 
 
Module stability means the interface for a Swift 5 library will work with Swift 6 compiler. more generally, the interface for a library should be forward-compatible with future versions of the compiler. this is useful following ways,
  • Can test a new compiler without rebuilding all of an app's dependencies.
  • May overlap with work to make the debugger work across Swift versions. 
  • May help reduce incremental build time by better tracking cross-target dependencies
  • Support for general non-resilient binary frameworks

Binary Compatibility

 
On Apple platforms, since the ABI is now stabilized, Swift 5.1 is binary compatible with Swift 5.0 and is binary compatible with future releases of Swift.
 
On non-Apple platforms (such as Linux) the ABI is not yet fully stable in order to allow for more vetting. Such vetting will be particularly needed for the new platforms based on Linux.
 

Source Compatibility

 
As with Swift 5.0, we expect that majority of sources that built with the Swift 5.0 compile with the Swift 5.1 compiler. However, it is possible that a bug fix in Swift 5.1 may cause it to detect errors in code that were not detected before.
 
In Swift 5.1 the following features were introduced,
  • Improvement to synthesized memberwise initializers. 
  • Implicit returns from single-expression functions. 
  • Universal Self
  • Opaque return types
  • Static and class subscripts
  • Warnings for ambiguous none cases
  • Matching optional enums against non-optionals
  • Ordered collections diffing
  • Creating uninitialized arrays

Improvement to synthesized memberwise initializers

 
Memberwise initializers were automatically created to accept parameters matching the property of a struct.
  1. struct Student {  
  2.     var name: String  
  3.     var fee: Int = 1500  
  4. }  
  5. let student = Student(name:"Ayush Dubey", fee:2000)  
The memberwise initializer now uses default parameter values for any properties that have them. In the Student struct, we've given a fee default as 1500, which means we can either specify it or leave it to the memberwise initializer.
  1. let student = Student(name:"Ayush Dubey", fee:2000)  
  2. let studentObj = Student(name:"Ayush Dubey")   

Implicit returns from single-expression functions

 
Single expression functions that return a value can now remove the return keyword and Swift will understand it implicitly.
  1. func double(_ number: Int) -> Int {  
  2.     number * 2  
  3. }  


Similar Articles