Robert Jaros
12/24/2022, 12:50 PMhandleInput
function is defined as returning Unit
and that makes the compiler complain when my handler is returning some nullable value.Robert Jaros
12/24/2022, 12:52 PM?: Unit
to get the code compiled:
override suspend fun InputHandlerScope<MainContract.Inputs, MainContract.Events, MainContract.State>.handleInput(
input: MainContract.Inputs
) = when (input) {
is MainContract.Inputs.SetTozsamosc -> {
manager.getTozsamosc(input.tozsamoscId)?.let {
postInput(it)
} ?: Unit
}
}
Robert Jaros
12/24/2022, 12:53 PMhandleInput
signature so it returns Unit?
instead?Casey Brooks
01/04/2023, 4:13 PMUnit
is the universal way to denote “this method doesn’t return anything useful”, and I can’t recall any other libraries/functions returning Unit?
.
But probably the bigger reason for not changing it is actually more about the proper usage of Ballast. Being built on the MVI model, which is at its heart Functional Programming, part of the “contract” is that the same State and Input produce the same result. In your snippet, that’s not necessarily true if manager.getTozsamosc
returns null. So Ballast tries to keep you doing the “right thing” with its type system and error-checking, and in fact, running the null-case here should throw an error like Input was not handled properly...
The solution is to either explicitly mark that code path with noOp()
to acknowledge that you are handling this case and didn’t just miss a code path, or else to move that whole manager.getTozsamosc
call into a sideJob
. postInput()
directly within the handleInput
scope is technically an extension function that posts the Input from a sideJob anyway, so this is probably the more correct solution for this situation.Robert Jaros
01/04/2023, 4:30 PMUnit?
problem from my code, so it's no longer an issue for meRobert Jaros
01/04/2023, 4:39 PMRobert Jaros
01/04/2023, 4:40 PMRobert Jaros
01/04/2023, 4:40 PMRobert Jaros
01/04/2023, 4:42 PMCasey Brooks
01/04/2023, 4:57 PMupdateState { }
, rather than moving it to a sideJob and posting an Input back with the result. Just be aware of what suspending in the InputHandler means for the Input queue and your InputStrategy.