Created
October 21, 2016 00:14
-
-
Save khanlou/60c4fd0baa457b7a621a74ee599be867 to your computer and use it in GitHub Desktop.
Revisions
-
khanlou revised this gist
Oct 21, 2016 . No changes.There are no files selected for viewing
-
khanlou created this gist
Oct 21, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ protocol State {} struct Transition<From: State, To: State> { let transition: () -> To } struct Machine<CurrentState: State> { var state: CurrentState func transition<To>(with transition: Transition<CurrentState, To>) -> Machine<To> where To: State { return Machine<To>(state: transition.transition()) } } struct Pending: State {} struct Loading: State {} struct Loaded: State {} struct Failed: State {} let kickoff = Transition<Pending, Loading>(transition: { return Loading() }) let finishLoading = Transition<Loading, Loaded>(transition: { return Loaded() }) let machine = Machine<Pending>(state: Pending()) let newMachine = machine.transition(with: kickoff) // compiles newMachine.transition(with: finishLoading) machine.transition(with: finishLoading) // Cannot convert value of type 'Transition<Loading, Loaded>' to expected argument type 'Transition<Pending, _>'