I was reading documentation about Swift Protocol Composition and found this: https://docs.swift.org/swift-book/LanguageGuide/Protocols.html#:~:text=Reference%20Types.-,Protocol%20Composition,-It%20can%20be
It can be useful to require a type to conform to multiple protocols at the same time. You can combine multiple protocols into a single requirement with a protocol composition. Protocol compositions behave as if you defined a temporary local protocol that has the combined requirements of all protocols in the composition. Protocol compositions don’t define any new protocol types.
Protocol compositions have the form SomeProtocol & AnotherProtocol. You can list as many protocols as you need, separating them with ampersands (&). In addition to its list of protocols, a protocol composition can also contain one class type, which you can use to specify a required superclass.
Here’s an example that combines two protocols called Named and Aged into a single protocol composition requirement on a function parameter:
protocol Named {
var name: String { get }
}
protocol Aged {
var age: Int { get }
}
struct Person: Named, Aged {
var name: String
var age: Int
}
func wishHappyBirthday(to celebrator: Named & Aged) {
print("Happy birthday, \(celebrator.name), you're \(celebrator.age)!")
}
let birthdayPerson = Person(name: "Malcolm", age: 21)
wishHappyBirthday(to: birthdayPerson)
// Prints "Happy birthday, Malcolm, you're 21!"
Could someone maybe explain the difference between the ampersand and the comma when stating that a type conforms to two or more protocols? I tested this in Playgrounds and both the ampersand and comma worked just fine. I tried doing some research online but didn't really find a good answer for this..