Skip to content

Instantly share code, notes, and snippets.

@marty-suzuki
Created July 21, 2020 04:36
Show Gist options
  • Save marty-suzuki/84ded686a5a238fa8a0468f20a765960 to your computer and use it in GitHub Desktop.
Save marty-suzuki/84ded686a5a238fa8a0468f20a765960 to your computer and use it in GitHub Desktop.
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