Last active
January 19, 2017 09:45
-
-
Save ukitaka/91788ad0fed11e3e55d09e3551c6c027 to your computer and use it in GitHub Desktop.
PhantomTypeをSwiftでもなんとかして使いたい
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 characters
| protocol LoginState { } | |
| final class Login: LoginState { } | |
| final class Logout: LoginState { } | |
| class LoginStateAction<State>: LoginStateActionType { } | |
| protocol LoginStateActionType { | |
| func loginState() -> LoginStateAction<Login>? | |
| func logoutState() -> LoginStateAction<Logout>? | |
| } | |
| extension LoginStateActionType { | |
| func loginState() -> LoginStateAction<Login>? { | |
| return self as? LoginStateAction<Login> | |
| } | |
| func logoutState() -> LoginStateAction<Logout>? { | |
| return self as? LoginStateAction<Logout> | |
| } | |
| } | |
| extension LoginStateAction where State: Login { | |
| func logout() -> LoginStateAction<Logout> { | |
| return LoginStateAction<Logout>() | |
| } | |
| } | |
| extension LoginStateAction where State: Logout { | |
| func login() -> LoginStateAction<Login> { | |
| return LoginStateAction<Login>() | |
| } | |
| } | |
| let action: LoginStateActionType = LoginStateAction<Login>() // !!! ここを実現するためにassociatedTypeを使わない | |
| action.loginState()?.logout() | |
| action.logoutState()?.login() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
状態がふえるときつそう