Where Clauses
Type constraints, enable you to define requirements on the type parameters associated with a generic function or type.
It can also be useful to define requirements for associated types. You do this by defining where clauses as part of a type parameter list. A where clause enables you to require that an associated type conforms to a certain protocol, and/or that certain type parameters and associated types be the same. You write a where clause by placing the where
keyword immediately after the list of type parameters, followed by one or more constraints for associated types, and/or one or more equality relationships between types and associated types.
The example below defines a generic function called allItemsMatch
, which checks to see if two Container
instances contain the same items in the same order. The function returns a Boolean value of true
if all items match and a value of false
if they do not.
The two containers to be checked do not have to be the same type of container (although they can be), but they do have to hold the same type of items. This requirement is expressed through a combination of type constraints and where clauses:
func allItemsMatch<C1: Container, C2: Container where C1.ItemType == C2.ItemType, C1.ItemType: Equatable>(someContainer: C1, _ anotherContainer: C2)->Bool {
// check that both containers contain the same number of items
if someContainer.count != anotherContainer.count {
return false
}
// check each pair of items to see if they are equivalent
for i in 0..<someContainer.count {
if someContainer[i] != anotherContainer[i] {
return false
}
}
// all items match, so return true
return true
}
This function takes two arguments called someContainer
and anotherContainer
. The someContainer
argument is of type C1
, and the anotherContainer
argument is of type C2
. Both C1
and `C2 are type parameters for two container types to be determined when the function is called.
The function's type parameter list places the following requirements on the two type parameter:
-
C1
must conform to theContainer
protocol (written asC1: Container
). -
C2
must also conform to theContainer
protocol (written asC2: Container
). - The
ItemType
forC1
must be the same as theItemType
forC2
(written asC1.ItemType == C2.ItemType
). - The
ItemType
forC1
must conform to theEquatable
protocol (written as `C1.ItemType: Equatable).
The third and fourth requirements are defined as part of a where clause, and are written after the where
keyword as part of the function's type parameter list.
These requirements mean:
-
someContainer
is a container of TypeC1
. -
anotherContainer
is a container of typeC2
. -
someContainer
andanotherContainer
contain the same type of items. - The items in
someContainer
can be checked with the not equal operator (!=) to see if they are different from each other.
The third and fourth requirements combine to mean that the items in anotherContainer
can also be checked with the !=
operator, because they are exactly the same type as the items in someContainer
.
These requirements enable the allItemsMatch(_:_:)
function to compare the two containers, even if they are of a different container type.
The allItemsMatch(_:_:)
function starts by checking that both containers contain the same number of items. If they contain a different number of items, there is no way that they can match, and the function returns false
.
After making this check, the function iterates over all of the items in someContainer
with a for-in
loop and the half-open range operator (..<). For each item, the function checks whether the item from someContainer
is not equal to the corresponding item in anotherContainer
. If the two items are not equal, then the two containers do not match, and the function returns false
.
If the loop finishes without finding a mismatch, the two containers match, and the function returns true
.
Here's how the allItemsMatch(_:_:)
function looks in action:
var stackOfStrings = Stack<String>()
stackOfStrings.push("uno")
stackOfStrings.push("dos")
stackOfStrings.push("tres")
var arrayOfStrings = ["uno", "dos", "tres"]
if allItemsMatch(stackOfStrings, arrayOfStrings) {
print("All items match.")
}else {
print("Not all items match.")
}
// prints "All items match."
The example above creates a Stack
instance to store String
values, and pushes three strings onto the stack. The example also creates an Array
instance initialized with an array literal containing the same three strings as the stack. Even though the stack and the array are of a different type, they both conform to the Container
protocol, and both contain the same type of values. You can therefore call the allItemsMatch(_:_:)
function with these two container as its arguments. In the example above, the allItemsMatch(_:_:)
function correctly reports that all of the items in the two containers match.