O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The where clauses

The where clause can be used to define more complex type constraints, for instance, to conform to more than one protocol with some constraints.

We can specify additional requirements on type parameters and their associated types by including a where clause after the Generic parameter list. A where clause consists of the where keyword, followed by a comma-separated list of one or more requirements.

For instance, we can express the constraints that a Generic type T inherits from a C class and conforms to a V protocol as <T where T: C, T: V>.

We can constrain the associated types of type parameters to conform to protocols. Let's consider the following Generic parameter clause:

 <Seq: SequenceType where Seq.Generator.Element: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required