-
Notifications
You must be signed in to change notification settings - Fork 25
Closed
Labels
Description
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?