title: Protocol with associated type. How to avoid to manually assign a type on all the implementations of the protocol
body: <p>I am doing my own Redux/Flux easy implementation and I am trying to model some data structures.</p>
  
  <p>Basically I have a state protocol that can have an associated type action. </p>
  
  <pre><code>protocol State {
      associatedtype ActionType: StateAction
  } 
  </code></pre>
  
  <p>Then I define my states like: </p>
  
  <pre><code>struct BoxState: State {
      typealias ActionType = Action
      var boxes:[Box]
  }
  
  extension BoxState {
      enum Action: StateAction {
          case select(String)
          case reshape(String, CGRect)
          case delete(String)
          case add(CGPoint)
      }
  }
  </code></pre>
  
  <p>What I dont like from this is that I will have to define the <code>associatedtype</code> for each implementation of the protocol, even when every enum will have the same type. </p>
  
  <p>1 - Is there a way to force each State implementation to have an enum type <code>Action</code>?. I understand that <code>Action</code> is actually of type <code>BoxState.Action</code>, but maybe there is a way to abstract it even better. </p>
  
  <p>Edit: Big explanation, not needed for the question above, but might serve as explanation: 
  I started doing an easy Redux implementation, because I like the reducer functions and the views reacting to a state. </p>
  
  <p>As suggested here, as suggested here, a store should dispatch actions. 
  This is my very rough implementation so far: </p>
  
  <pre><code>protocol StateAction {}
  
  protocol State {
      associatedtype ActionType: StateAction
  }
  
  protocol Dispatcher {
      associatedtype Action: StateAction
      func dispatch(_ action: Action)
  }
  
  class StateStore&lt;Type: State&gt;: Dispatcher {
  
      typealias Action = Type.ActionType
  
      private var subscribers:[(Type)-&gt;()] = []
      var onDispatch:((Action)-&gt;())?
  
      var state: Type! {
          didSet {
              self.didUpdateState(state)
          }
      }
  
      func didUpdateState(_ state: Type) {
          self.subscribers.forEach { $0(state) }
      }
  
      func addSubscriber(_ subscriber: @escaping (Type)-&gt;())  {
          self.subscribers.append(subscriber)
      }
  
      func dispatch(_ action: Action) {
          self.onDispatch?(action)
      }
  }
  </code></pre>
  
  <p>What I think is wrong is that the <code>State</code> is defining <code>Action</code>s. The views are the ones triggering actions, and those actions should be handled to mutate one or more states. Still the "technical" problem explained in the beginning might have a solution. </p>