Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. protocol State {}
  2.  
  3. struct Transition<From: State, To: State> {
  4. let transition: () -> To
  5. }
  6.  
  7. struct Machine<CurrentState: State> {
  8.  
  9. var state: CurrentState
  10.  
  11. func transition<To>(with transition: Transition<CurrentState, To>) -> Machine<To> where To: State {
  12. return Machine<To>(state: transition.transition())
  13. }
  14. }
  15.  
  16. struct Pending: State {}
  17. struct Loading: State {}
  18. struct Loaded: State {}
  19. struct Failed: State {}
  20.  
  21. let kickoff = Transition<Pending, Loading>(transition: { return Loading() })
  22.  
  23. let finishLoading = Transition<Loading, Loaded>(transition: { return Loaded() })
  24.  
  25. let machine = Machine<Pending>(state: Pending())
  26.  
  27. let newMachine = machine.transition(with: kickoff) // compiles
  28. newMachine.transition(with: finishLoading)
  29. machine.transition(with: finishLoading) // Cannot convert value of type 'Transition<Loading, Loaded>' to expected argument type 'Transition<Pending, _>'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement