although I have post it in the general, I trust yo...
# arrow
c
although I have post it in the general, I trust your wisdom 😀
h
If I understand correctly, there is a conceptual error behind the compilation error. When you say that
Copy code
val 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.
What should
a
do when called with a
GetTodo
action?
c
In this case, the State never change due to not exist a reducer able to transform the action in a new State
h
Yes but you need to tell it to the compiler 😅 You can do something like this:
Copy code
inline 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()
c
yes, you are right , those was my solution 😆
Copy code
fun interface Reducer<S : StoreState> {
    operator fun invoke(state: S, action: Action): S
}
Copy code
fun interface ReducerType<S : StoreState, A : Action> {
    operator fun S.invoke(action: A): S
}
Copy code
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
        }
    }
Copy code
val TodoReducer: Reducer<TodosState> =
    Reducer<TodosState, TodoListAction> { action ->
        when (action) {
            is LoadTodos -> this
        }
    }
c
not strictly related, but are you doing some kotlinscript? this looks so much like ngrx
c
I'm trying to do a Redux implementation for an Android application
c
ah i see, interesting
c
Maybe I'll publish the final implementation. (if everything works 😆)
c
seems like a lot of work, but i guess that's life when you're living on the edge
c
if i tell you the truth i'm on vacation 🤦 , but it's fun for me haha
c
😄