Swift is a Protocol-oriented Language

This video from WWDC 2015 has really been resonating with me recently.

I’ve been transitioning a couple of large projects from class-based, Objective-C code to a more Struct-based, Swift approach. The revelation comes in the form of embracing protocol-oriented design and what that means for the things that I do.

These projects are all music based. They all need to represent music in some way. So I have been writing a Music representation library to unify and codify the things that I need to do. But here’s an example of the embrace of the protocol:

MusicTransposable Protocol

Music pitches can be transposed, or moved, in order to represent other pitches. For example, C0 transposed by a Major Third becomes E0. The MusicTransposable protocol should be adopted by any type that can be transposed by an interval. Default implementations are available for MusicPitch and types that adopt MusicPitchCollection (i.e. MusicChords can be transposed by transposing their individual pitches).

Here’s how it works:

From here, we can extend MusicPitch to adopt the protocol, using MusicInterval’s destinationPitch method to compute a destination pitch from a root pitch:

But what about MusicPitchCollection, a protocol for managing a collection of pitches, such as a Scale or Chord:

Notice that I am requiring MusicPitchCollection adoptees to also adopt MusicTransposable. But since they are simply collections of pitches, which are already transposable, maybe they should just transpose their individual pitches:

It should be clear by now that any collection of pitches adopts MusicPitchCollection and immediately is able to transpose those pitches without writing any extra code beyond a declaration of an array to store those pitches. This kind of out-of-the-box functionality is amazing.

The next big step involves rewriting the drawing code that displays notes and rhythms. Stay tuned, because protocol adoption makes this problem much more general and involves a much cleaner codebase that was just not possible before.

Leave a Reply

Your email address will not be published. Required fields are marked *