Skip to content

Instantly share code, notes, and snippets.

@oleksii-demedetskyi
Created June 14, 2019 13:26
Show Gist options
  • Save oleksii-demedetskyi/cbf1b339ee407838e6597dbe00f9ecc3 to your computer and use it in GitHub Desktop.
Save oleksii-demedetskyi/cbf1b339ee407838e6597dbe00f9ecc3 to your computer and use it in GitHub Desktop.
protocol _Reducer {
associatedtype State
associatedtype Action
func reduce(state: inout State, action: Action)
}
protocol Reducer: _Reducer where State == Body.State, Action == Body.Action {
associatedtype Body: Reducer
var body: Body { get }
}
extension Reducer {
func reduce(state: inout State, action: Action) {
body.reduce(state: &state, action: action)
}
}
struct ConcreteReducer<State, Action>: Reducer {
let performReduce: (inout State, Action) -> Void
var body: Self { self } // ???
func reduce(state: inout State, action: Action) {
performReduce(&state, action)
}
}
// ERROR: 'Reducer' requires the types 'Action' and '
// (some Reducer).Action' be equivalent
struct EmptyRedcer<State, Action>: Reducer {
var body: some Reducer {
ConcreteReducer<State, Action> { state, action in
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment