What is difference between '&' and ',' in swift about Protocol Composition

Viewed 159

I have a question

In swift,

class Card : Equatable & CustomStringConvertible

and

class Card : Equatable, CustomStringConvertible

I think there are doing the same things. What is different??

1 Answers

From the Swift documentation on protocols, I think & is mainly used when you are requiring a variable to adopt to multiple protocols, ex:

func wishHappyBirthday(to celebrator: Named & Aged) {
    print("Happy birthday, \(celebrator.name), you're \(celebrator.age)!")
}

And you use , when you want to adopt a custom struct/class to multiple protocols:

struct SomeStructure: FirstProtocol, AnotherProtocol {
    // structure definition goes here
}
Related