Created
July 21, 2020 04:36
-
-
Save marty-suzuki/84ded686a5a238fa8a0468f20a765960 to your computer and use it in GitHub Desktop.
This file contains 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
enum _Value<Initial, Changed> { | |
case initial(Initial) | |
case changed(Changed) | |
} | |
typealias StateRepresentable<T> = _Value<T, T> | |
typealias ChangeHandleable<T> = _Value<Void, T> | |
extension _Value where Initial == Void { | |
static func initial() -> _Value<Void, Changed> { | |
.initial(()) | |
} | |
func callAsFunction() -> Changed? { | |
switch self { | |
case .initial: | |
return nil | |
case let .changed(value): | |
return value | |
} | |
} | |
} | |
extension _Value where Initial == Changed { | |
func callAsFunction() -> Changed { | |
switch self { | |
case let .initial(value), | |
let .changed(value): | |
return value | |
} | |
} | |
} | |
var value1 = ChangeHandleable<Int>.initial() | |
value1 = .changed(1) | |
print(String(describing: value1())) | |
var value2: StateRepresentable<Int>? = .initial(0) | |
value2 = nil | |
value2 = .changed(1) | |
print(String(describing: value2?())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment