Maybe I'm approaching it the wrong way, but consider the following.
class MyClass {
typealias Schema = StateMachineSchema<State, Event, ()>
private var stateMachine: StateMachine<Schema>
enum State {
case StateOne, StateTwo
}
enum Event {
case EventOne, EventTwo
}
init() {
stateMachine = StateMachine(
schema: Schema(
initialState: .StateOne,
transitionLogic: { (state, event) in
switch state {
case .StateOne:
switch event {
case .EventOne: return nil
case .EventTwo: return nil
}
case .StateTwo:
switch event {
case .EventOne: return nil
case .EventTwo: return nil
}
}
}
),
subject: ()
)
}
func foo() {
// I can do that because stateMachine is a var.
// But I can't make it a let because handleEvent() is mutating and let disallows that
stateMachine = StateMachine(
schema: Schema(
initialState: .StateOne,
transitionLogic: { (state, event) in
switch state {
case .StateOne:
switch event {
case .EventOne: return (.StateOne ,nil)
case .EventTwo: return (.StateTwo ,nil)
}
case .StateTwo:
switch event {
case .EventOne: return (.StateOne ,nil)
case .EventTwo: return (.StateTwo ,nil)
}
}
}
),
subject: ()
)
}
}
Here I have a class with a private stateMachine. The problem lies in bar() where I can replace the stateMachine with a completely different one, thus overriding the flow.
I cannot make it a let because then calling handleEvent() would be disallowed because it's mutating.
If StateMachine was a class, the we could make it private let stateMachine: StateMachine<Schema>, initialize it once and then replacing it with another one would not be possible but handleEvent() would still work.
What do you think?
Maybe I'm approaching it the wrong way, but consider the following.
Here I have a class with a private
stateMachine. The problem lies inbar()where I can replace thestateMachinewith a completely different one, thus overriding the flow.I cannot make it a
letbecause then callinghandleEvent()would be disallowed because it'smutating.If
StateMachinewas aclass, the we could make itprivate let stateMachine: StateMachine<Schema>, initialize it once and then replacing it with another one would not be possible buthandleEvent()would still work.What do you think?