In our last post we discussed protocols and how we as human beings conform to them all the time in different social situations: you’re favourite place to be, right? Today we’re going back to our work night out scenario where Hamish and Merrida are about to bump into one another for the first time. They both conform to the SmallTalking protocol, which means at any given time we can call their ‘makePoliteChitChat’ function. How they implement that method may vary, but they are chatting nonetheless.

However there is a twist! Hamish, you see, has a lot of social chatting experience and he also conforms to the ‘RemovingAwkwardSilence’ protocol which Merida does not yet conform to:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
protocol RemovingAwkwardSilence {
   // Return an array of interesting topics!
   func fillWeirdSilence() -> [InterestingTopic]
}
struct Hamish: SmallTalking, RemovingAwkwardSilence {
   // Very impressive topics xD
   var coolArrayOfInterestingTopics = [
      InterestingTopic(.AztecSocialNorms),
      InterestingTopic(.ElvishTop100Jokes),
      InterestingTopic(.StarWarsOrStarTrek)
  ]

   func fillWeirdSilence() -> [InterestingTopic] {
      return self.coolArrayOfInterestingTopics
   {
}

Anytime there is an awkward silence Hamish can fill the gap with some prepared and fascinating conversational topics! If only life was always this simple when in social situations. What’s going on here is that Hamish conforms to both protocols: he has inherited two separate skills. You can’t do this with classes and rather than giving him tonnes of stuff he doesn’t need, like being able to juggle, instead he has just the right skills to make a good impression. Merrida, however, does not conform to this protocol, so she would silently listen whether he talked or not.

The wonderful thing about protocols is that a struct, or class can implement as many protocols as needed and like a jigsaw, we begin to piece functionality together.