Benoît
03/09/2021, 4:45 PMCLOVIS
03/09/2021, 5:04 PMFlow
? If my understanding is correct, as long as you don't use exceptions in it, it should be fineBenoît
03/09/2021, 5:13 PMraulraja
03/09/2021, 5:43 PMunsafePerformIO
would be in a IO based program. Ultimately if you want pure stream composition across the board the UI itself would need to be inside Flow and you’d need control to write the main
entry point to the program. That is probably not ergonomic or possible in Android so without looking at code I’d say you are already ok unless there is some specific code.
The fact that a flow may contain Unit denotes a side effecting op inside the stream but does not make it impure. When the stream is consumed if it ends on Unit
then that is the final value of the “Stream program” for this case.raulraja
03/09/2021, 5:47 PMBenoît
03/09/2021, 6:09 PMUnit
.
My Flows don't return Unit, but the typical implementation of a Flow is a set of functions that make a Flow emit specific items.
For example, one would call a function fun fetchData() : Unit
and somewhere else in the code, a collect
block would be listening for data.
This patterns allows a new screen to stay up to date with a call that another/previous screen has triggered.
But this is not really functional is it?
Currently, my ViewModel calls the repository's suspend function returning an ApiResponse<T>
, which allows me to create a pure function that returns a UI intent, given a specific UI input. The problem with this approach is that, since there's no side effect, it's impossible for a new screen to get updated when the response comes backraulraja
03/09/2021, 6:14 PMsuspend
then it’s functional because suspend
in the same way IO and other functional streams guarantees that if the program results in an exception is captured by the context of the suspend
continuation.
We should keep in mind that if we ever wanted to take this to the extreme we can also assert that suspend fun
is a model for continuations. It has been proven several times ContT or the Continuation monad can model all effects and other monads so technically Kotlin is the place where people without knowing often practice principled IO and effect tracked programming.Benoît
03/09/2021, 6:26 PMfetchData() : Unit
function and a val dataFlow : Flow<Data>
pattern? So far it's the best way I have found for new subscriber to be notified of an ongoing call, but since it's not functional I'm worried about using it, rather than a pure function. Ideally I'd like the best of both world, having 1 function that returns an ongoing call if there's one, or makes a new call if there's noneraulraja
03/10/2021, 9:09 AMsuspend
then it’s ok. Also if Unit is confusing you can always create something like object Ok
and use it in place of Unit
to be explicit in the effect use sites as to what the return value is.Benoît
03/10/2021, 12:01 PM