Swift 4.1, available in Xcode 9.3, brings a number of improvements to the language, including automatic implementation of the Equatable and Hashable protocols, conditional conformance, and more.
Swift 4.1, available in Xcode 9.3, brings a number of improvements to the language, including automatic implementation of the Equatable and Hashable protocols, conditional conformance, and more.
Conditional conformance
Conditional conformance allows to express the idea that a generic type will conform to a protocol only if its type parameters satisfy certain requirements. For example an Array can implement the Equatable protocol only if its elements also conform to it, which can be expressed in the following declaration:
This closes a gap in Swift generics, fully enabling the possibility of generic adapter types, i.e., generic types that reflect the capabilities of the composing types. For example, in Swift 3 there exist four different generic types that handle “lazy” items: LazySequence, LazyCollection, LazyBidirectionalCollection, and LazyRandomAccessCollection. Each of those types reimplements, or forwards, the same API as the composing type. Using conditional conformance, a Sequence of a lazy type can be declared as conforming to the lazy protocol without defining any intermediate generic types.
Synthesized == and hashValue
Swift 4.1 compiler is able to synthesize the implementation of the == and hashValue methods for structs and enums that declare conformance to the Equatable and Hashable protocols. Previously, developers had to provide a custom implementation of those methods, which was a rather tedious task: the == method implementation had, in the general case, to compare each corresponding member of the structs or unions, while the hashValue had to somehow ensure that it returned a unique string. In Swift 4.1, developers can just declare conformance to those protocols and the relevant methods will be automatically synthesized, provided all stored properties or enum cases conform to Equatable or Hashable respectively.
Other significant changes in Swift 4.1 are the following:
For a full list of all changes, you can refer to the official change log .