Now that protocols have been explained it is a good time to introduce the concept of opaque return types. As we have seen in previous chapters, if a function returns a result, the type of that result must be included in the function declaration. The following function, for example, is configured to return an Int result:
func doubleFunc1 (value: Int) -> Int {
return value * 2
}
Instead of specifying a specific return type (also referred to as a concrete type), opaque return types allow a function to return any type as long as it conforms to a specified protocol. Opaque return types are declared by preceding the protocol name with the some keyword. The following changes to the doubleFunc1() function, for example, declare that a result will be returned of any type that conforms to the Equitable protocol:
func doubleFunc1(value: Int) -> some Equatable {
value * 2
}
To conform to the...