I have a StateMachine , which is universal, which allows implementing various sets of states, for example, an enumeration. I want to use the StateMachineDelegate protocol to notify the delegate when the state machine goes into a new state.
But this does not work, as the delegate protocol is also common with type requirements. The error shows where the delegate property is declared.
protocol StateType: Hashable {} protocol StateMachineDelegate: class { typealias S: StateType func stateMachine(stateMachine: StateMachine<S>, didEnterState newState: S) } class StateMachine<S: StateType> { typealias State = S weak var delegate: StateMachineDelegate?
I need to somehow bind this to StateMachineDelegate.S == S in the StateMachine , but I'm not sure how to do this, or if it's possible. I tried:
class StateMachine<S: StateType, D: StateMachineDelegate where DS == S> { ... weak var delegate: D? ... }
but then I get stuck trying to rework the protocol to correctly declare a generic StateMachine type. And it does not seem right to declare a delegate type before creating StateMachine .
generics swift protocols delegates
Stuart
source share