title: What's the correct way to implement default properties when doing default implementation of protocols in Swift?
body: <p>I am truly fascinated by the concept of Protocol-Oriented Programming in Swift, and because of that, I'm moving an old project I created last year (which was originally an OOP framework) to POP.</p>
  
  <p>At this stage, the problems I'm having could be because I'm either understanding POP incorrectly, or the Swift 2.0 Betas don't have everything to create a truly Protocol-Oriented framework (not very likely - If anything I might be misunderstanding certain aspects of POP).</p>
  
  <p>Protocol-Oriented Programming is a whole new programming paradigm introduced to the world less than a month ago, so there is not much written content about it (the <a href="http://www.raywenderlich.com/109156/introducing-protocol-oriented-programming-in-swift-2" rel="nofollow">only tutorial</a> I found on the topic doesn't address the problem I'm having, and the WWDC video didn't either).</p>
  
  <p>Anyway, to the point: I am either doing something wrong here, or one of the downsides of Protocol-Oriented Programming is that you are bound to repeat a lot of code. Case in point:</p>
  
  <p>I have the following protocol that has many properties and also complies with the <code>Equatable</code> protocol:</p>
  
  <pre><code>protocol MediaType : Equatable {
  
      /// MARK: - Properties
  
      /// The ID of the media, as assigned by MyAnimeList.
      var ID: Int { get }
  
      /// The title of the media.
      var title: String { get }
  
      /// Other titles by which this anime may be commonly known (only available in details requests).
      var otherTitles: (synonyms: [String], english: [String], japanese: [String])? { get }
  
      /// The global ranking of the title (only available in anime details requests).
      var rank: Int? { get }
  
      /// Rank of the title based on popularity (number of people adding title to the list) (only available in details requests).
      var popularityRank: Int? { get }
  
      /// URL to a representative image of the title. Usually a "cover" image.
      var imageURL: String { get }
  
      /// A list of adaptations of this media, or other media on which this media is based (only available in details requests).
      var adaptations: Relationships { get }
  
      /// The user's rating of the media.
      var memberScore: Float { get }
  
      /// Number of MyAnimeList members that that added the title to their list (only available in details requests).
      var membersCount: Int? { get }
  
      /// The number of MyAnimeList members that have this title on their favorites list (only available in details requests).
      var favoritedCount: Int? { get }
  
      /// A short HTML-formatted description of the media.
      var synopsis: String { get }
  
      /// A list of genres for this title (only available in details requests).
      var genres: [String]? { get }
  
      /// Popular tags for the title as assigned by MyAnimeList members (only available in details requests).
      var tags: [String] { get }
  }
  </code></pre>
  
  <p>In the original version of my framework, this protocol was a class called <code>Media</code>, and two other classes inherited from it. So they got all those properties for free.</p>
  
  <p>But it doesn't look like I can give my objects that comply with that protocol a default implementation (namely, getters) for those properties?</p>
  
  <p>The first thing I tried, which is to simply give the protocol to my struct declaration, failed, which was to be expected as I wasn't providing any implementation for the properties:</p>
  
  <pre><code>struct Anime : MediaType {
  
      /// MARK: - MediaType
  
  }
  
  /// Compares two Anime_ objects. Two Anime_ objects are considered equal when they have the same ID and title.
  func ==(lhs: Anime, rhs: Anime) -&gt; Bool {
      return (lhs.ID == rhs.ID) &amp;&amp; (lhs.title == rhs.title)
  }
  </code></pre>
  
  <p>This fails with:</p>
  
  <blockquote>
    <p>Type 'Anime' does not conform to protocol 'MediaType'</p>
  </blockquote>
  
  <p>My next attempt was to write an extension for MediaType, and throw the properties there:</p>
  
  <pre><code>extension MediaType {
      /// The ID of the media, as assigned by MyAnimeList.
      let ID: Int
  
      /// The title of the media.
      let title: String
  
      /// Other titles by which this anime may be commonly known (only available in details requests).
      let otherTitles: (synonyms: [String], english: [String], japanese: [String])?
  
      /// The global ranking of the title (only available in anime details requests).
      let rank: Int?
  
      /// Rank of the title based on popularity (number of people adding title to the list) (only available in details requests).
      let popularityRank: Int?
  
      /// URL to a representative image of the title. Usually a "cover" image.
      let imageURL: String
  
      /// A list of adaptations of this media, or other media on which this media is based (only available in details requests).
      let adaptations: Relationships
  
      /// The user's rating of the media.
      let memberScore: Float
  
      /// Number of MyAnimeList members that that added the title to their list (only available in details requests).
      let membersCount: Int?
  
      /// The number of MyAnimeList members that have this title on their favorites list (only available in details requests).
      let favoritedCount: Int?
  
      /// A short HTML-formatted description of the media.
      let synopsis: String
  
      /// A list of genres for this title (only available in details requests).
      let genres: [String]?
  
      /// Popular tags for the title as assigned by MyAnimeList members (only available in details requests).
      let tags: [String]
  }
  </code></pre>
  
  <p>This didn't work:</p>
  
  <blockquote>
    <p>Extensions may not contain stored properties.</p>
  </blockquote>
  
  <p>And it had the one downside that I really didn't like: I was already duplicating code, by copying the properties of the protocol into the extension.</p>
  
  <p>So in the end, I could never get my properties to be "spread" to the objects conforming to the protocol, so I ended up adding the properties to the <code>Anime</code> struct.</p>
  
  <pre><code>struct Anime : MediaType {
  
      /// MARK: - MediaType
  
      /// The ID of the media, as assigned by MyAnimeList.
      let ID: Int
  
      /// The title of the media.
      let title: String
  
      /// Other titles by which this anime may be commonly known (only available in details requests).
      let otherTitles: (synonyms: [String], english: [String], japanese: [String])?
  
      /// The global ranking of the title (only available in anime details requests).
      let rank: Int?
  
      /// Rank of the title based on popularity (number of people adding title to the list) (only available in details requests).
      let popularityRank: Int?
  
      /// URL to a representative image of the title. Usually a "cover" image.
      let imageURL: String
  
      /// A list of adaptations of this media, or other media on which this media is based (only available in details requests).
      let adaptations: Relationships
  
      /// The user's rating of the media.
      let memberScore: Float
  
      /// Number of MyAnimeList members that that added the title to their list (only available in details requests).
      let membersCount: Int?
  
      /// The number of MyAnimeList members that have this title on their favorites list (only available in details requests).
      let favoritedCount: Int?
  
      /// A short HTML-formatted description of the media.
      let synopsis: String
  
      /// A list of genres for this title (only available in details requests).
      let genres: [String]?
  
      /// Popular tags for the title as assigned by MyAnimeList members (only available in details requests).
      let tags: [String]
  
      /// MARK: - Anime
  
  
  }
  </code></pre>
  
  <p>And this seemed to work. But now I have my properties in both <code>MediaType</code> and <code>Anime</code>. In OOP, you avoid the duplicate code by subclassing.</p>
  
  <p>So I repeat my question here: Am I misunderstanding Protocol-Oriented Programming, or is the downside of POP that you have to copy-and paste your protocol-specific logic whenever you make a struct/class/enum comply with it?</p>