carbaj0
05/15/2021, 11:44 AMHieiJ
05/15/2021, 1:34 PMval a: Reducer<TodosState, TodoAction>
you are saying that a
will be able to handle any possible TodoAction
as input parameter, while the TodoReducer
you are trying to assign can't handle all the possible inputs.HieiJ
05/15/2021, 1:37 PMa
do when called with a GetTodo
action?carbaj0
05/15/2021, 1:58 PMHieiJ
05/15/2021, 7:19 PMinline fun <P : Action, reified A : P> Reducer<TodosState, A>.asMoreGenericReducer(): Reducer<TodosState, P> =
Reducer { state, action ->
when(action) {
is A -> this(state, action)
else -> state
}
}
val a: Reducer<TodosState, TodoAction> = TodoReducer.asMoreGenericReducer()
carbaj0
05/16/2021, 4:15 AMcarbaj0
05/16/2021, 4:15 AMfun interface Reducer<S : StoreState> {
operator fun invoke(state: S, action: Action): S
}
fun interface ReducerType<S : StoreState, A : Action> {
operator fun S.invoke(action: A): S
}
inline fun <S : StoreState, reified A : Action> Reducer(
reducer: ReducerType<S, A>
): Reducer<S> =
Reducer { state, action ->
when (action) {
is A -> reducer.run { state(action) }
else -> state
}
}
val TodoReducer: Reducer<TodosState> =
Reducer<TodosState, TodoListAction> { action ->
when (action) {
is LoadTodos -> this
}
}
Chris Paul
05/17/2021, 9:49 AMcarbaj0
05/17/2021, 11:15 AMChris Paul
05/17/2021, 11:16 AMcarbaj0
05/17/2021, 11:18 AMChris Paul
05/17/2021, 11:19 AMcarbaj0
05/17/2021, 11:23 AMChris Paul
05/17/2021, 11:24 AM