You can use a where clause which lets you specify as many requirements as you want (all of which must be fulfilled) separated by commas
Swift 2:
func someFunc<T where T:SomeProtocol, T:SomeOtherProtocol>(arg: T) {
// stuff
}
Swift 3 & 4:
func someFunc<T: SomeProtocol & SomeOtherProtocol>(arg: T) {
// stuff
}
or the more powerful where clause:
func someFunc<T>(arg: T) where T:SomeProtocol, T:SomeOtherProtocol{
// stuff
}
You can of course use protocol composition (e.g., protocol<SomeProtocol, SomeOtherProtocol>
), but it’s a little less flexible.
Using where
lets you deal with cases where multiple types are involved.
You may still want to compose protocols for reuse in multiple places, or just to give the composed protocol a meaningful name.
Swift 5:
func someFunc(arg: SomeProtocol & SomeOtherProtocol) {
// stuff
}
This feels more natural as the protocols are next to the argument.