title: Swift: Array of equatable generic type
body: <p>I have been the last days fighting with some issues regarding generics on Swift, and I don't find a way to figure out how to achieve this:</p>
  
  <ul>
  <li>I have a class <code>class Store&lt;S: State&gt;</code> where <code>State</code> is a simple protocol which extends Equatable <code>protocol State: Equatable</code>.</li>
  <li>I have a class <code>Logger</code> where I want to store an array of <code>Stores</code> to keep track of each change on them and compare their <code>State</code> with their old values, being able to check what changes in each iteration.</li>
  </ul>
  
  <p>For this, I need to store an array of  <strong>Any Kind of Store</strong> in my logger class. The problem comes when I try to use a <code>val storeArray = [Store&lt;Any&gt;]</code>, it doesn't work because Any is not an <code>Equatable</code> type, and I will need them to extend <code>Equatable</code> or <code>NSObject</code> to be able to compare the states between them.</p>
  
  <p>Is possible to achieve this in Swift? Or find out another way to compare 2 items without make a generic extend a <code>Equatable</code> protocol? </p>
  
  <p>In case you wanna check the implementation :</p>
  
  <p><strong>State :</strong></p>
  
  <pre><code>protocol State: Equatable {
  }
  </code></pre>
  
  <p><strong>Store :</strong></p>
  
  <pre><code>class Store&lt;S: State&gt; {
  
      private var _initialState: S
      private var _state: S
      private var processor = PublishSubject&lt;S&gt;()
  
      init(initialState: S) {
          _initialState = initialState
          _state = initialState
      }
  
      var state: S {
          get {
              return _state
          }
          set(value) {
              if (value != state) {
                  _state = value
                  processor.onNext(value)
              }
          }
      }
  
      func initialState() -&gt; S {
          return _initialState
      }
  
      /// Initialize the store. Called after all stores instances are ready.
      func initialize() {
          //TODO Check if its possible to force an override over a method
          preconditionFailure("This method must be overridden")
      }
  }
  </code></pre>
  
  <p>After the suggestion of <strong>Vadian</strong> I have tried to move it to a protocol with an associate type:</p>
  
  <pre><code>protocol Store: class {
      associatedtype State : StateDelegate
      var processor : PublishSubject&lt;State&gt; { get }
      var _state : State { get set }
      var state: State { get set }
      func initialState() -&gt; State
      func flowable() -&gt; Observable&lt;State&gt;
      func initialize() -&gt; Void
  }
  
  extension Store {
  
      var state: State {
          get {
              return _state
          }
          set(value) {
              if (value != state) {
                  _state = value
                  processor.onNext(value)
              }
          }
      }
  
      func flowable() -&gt; Observable&lt;State&gt; {
          return processor.startWith(state)
      }
  
      func initialState() -&gt; State {
          return State.init()
      }
  }
  </code></pre>
  
  <p>But I retrieve the next error when I try to create an <code>[Store]</code> array:
  <code>Protocol 'Store' can only be used as a generic constraint because it has Self or associated type requirements</code></p>