BT

Swift 4.1 Brings Conditional Conformance and More

| by Sergio De Simone Follow 14 Followers on Feb 11, 2018. Estimated reading time: 2 minutes |

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 the expression of 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:

extension Array: Equatable where Element: Equatable {
  static func ==(lhs: Array<Element>, rhs: Array<Element>) -> Bool { ... }
}

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

The 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:

A full list of all changes within Swift 4.1 can be found in the official change log.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT