I apologise for all these dumb questions, but anyw...
# coroutines
t
I apologise for all these dumb questions, but anyway.. Why does this work:
Copy code
override fun loadData() {
        launch {
            songRepository.getSongs(SongQuery.Album(name = album.name, albumArtistName = album.albumArtist))
                .collect { songs ->
                    this@AlbumDetailPresenter.songs = songs
                    view?.setData(songs)
                }
        }
    }
But this doesn’t:
Copy code
override fun loadData() {
        launch {
            songRepository.getSongs(SongQuery.Album(name = album.name, albumArtistName = album.albumArtist))
                .collect { songs ->
                    this@AlbumDetailPresenter.songs = songs
                }
            view?.setData(songs)
        }
    }
In the second case, shouldn’t 
view?.setData(songs)
 only get called after 
collect {}
 has completed?
o
yes, I think so, not sure how it would work out coroutine-wise otherwise
t
Well, it seems like it doesn’t get called at all
o
are you sure
view
is non-null?
not sure why it would differ between the top and bottom in that case though
t
Yes. But in the second case,
view?.setData(songs)
is not called (breakpoint not hit in debugger) - even if
view
was null I’d still expect the line to be hit
o
I would verify with a logging statement, sometimes the debugger isn't perfect for Kotlin in my experience
g
Probably collect never finish
o
that is a good point
g
so first works just because it receives result to collect and notify, but second never passed collect function
t
I see, that makes sense
g
Check that you don’t use something like StateFlow in your getSongs method, it never completes
t
I am indeed using StateFlow!
Thanks
I just need to use the first option. But this is another good little lesson in Coroutines
I’m slowly getting through all the gotchas
g
it’s general rule of reactive frameworks, if you have stream it’s better do not expect that it will complete, because it’s an implementation detail, it may return 0-Infinite number of elements, and can never complete
t
Yeah, I’m not actually expecting it to complete - just a mistake in the braces placement - and then I forgot that it doesn’t complete
👍 1
This is great. I think I have made the same error in a few places, so remembering not to assume
collect
will complete for streams is a good lesson
g
You also can use launchIn(), so you never have suspending collect, will work just as launching collection in background (exactly waht you are doing in this case by wrapping with launch), also makes it less nested:
Copy code
songRepository.getSongs(...)
   .onEach { setData(it) }
   .launchIn(scope)
👍 1
t
Oh yeah, that’s quite nice
So, are you saying never assume a Flow completes? Like, do you think it’s always a mistake to call
Copy code
launch {
    flow.collect {

    }
    doSomething()
}
Because that requires knowledge of whether the flow will complete, which, as you say, is an implementation detail?
g
not always mistake, but it means that you expect that you know contract of this flow, that it should complete at some point
t
I would usually use a suspend fun rather than a flow if I expect completion. But for some things, I want to emit elements along the way to completion
g
I wouldn’t call it mistake, sometimes you have a reson for this. For example recently in this Slack it was discussion about implementation of Spring Webflux API for database, which exposes select as stream of events, but not like usually you expect subsribe on data updates in table, but to make select of different rows lazy, so it completes when all rows are selected
so it’s not mistake there, but you should know contract of stream which you request
t
Yeah, I see what you’re saying
Thanks
g
But it may be a source of errors, especially when code changes, so you may be in situtation when code is changed but API of it is the same, it just exposes Flow
t
It would be nice if you could declare whether a Flow completes or not
g
But how would such type would look like?
t
Well, I’m not suggesting that the Coroutines API be changed, or that I have the answer. I just mean, it might be useful for this exact scenario
o
I suspect it would be somewhat similar to
Mono
in rx
g
It’s not only problem of coroutines, it’s true for any reactive-like type
Mono is too limited, and coroutines do not need mono, we already have suspend functions
t
Suspend functions aren’t the solution for this either - if you need intermediate emissions before completion
g
anyFlow..first() is the same as Mono
if you need intermediate emissions before completion
What do you mean?
My point is that if you need only single value (what Mono or Single doing), just use suspend function
t
Yeah, sounds like Mono is not the solution to this problem then
o
I was suggesting a more general form of Mono, which implies a
0..N
g
How Mono is the solution?
o
it isn't, never mind me
I'm not able to communicate what I mean, clearly
g
which implies a 
0..N
This is the same what Flow or rxjava Observable or Flux is and it exactly the same problem
j
Tests!
👌 1
☝️ 1
t
Sure, tests are great. And I’m not really advocating for a ‘CompletingFlow’ type… But, tests aren’t the answer to everything. Yes, they would help to catch this error and that’s great. But just because you can write a test to catch a possible bug, doesn’t mean we couldn’t also make it harder to write the bug in the first place.
g
again, I just don’t understand how such type may look like. Is it just the same Flow, but with promise in name to complete?
t
Maybe?
g
I understand Mono/Single/single, it’s 1 or error, Observable/Flux/Flow it’s 0..N, even Maybe it’s 0..1, but what is completing flow?
Maybe it’s just 0..1, or exactly the same as nullable suspend function
t
Yeah, perhaps theres no sense in marking a Flow as ‘completable’, if that simply means it might complete in 75 years from now
o
that can really be true for any suspending op though
is a suspend function "completable" if it calls
delay(Long.MAX_VALUE)
g
it’s true, and for any other function with Thread.sleep() or something like awaitTermination()
But case when Flow is not completing is much more common than never returning function (which ususally has some clue about it in the name), just because Flow often represent stream of events and it active until source of those events exists
t
I am just lamenting that there’s no way for us to codify that a flow can be expected to complete. I have written some flows that scan all the directories on the device, emitting the directory name for each one it finds. I know this completes, and I do something when it completes. And then some flows monitor a database and emit new values when the database changes - and I know these never complete. There’s no way for me to indicate that this is the behaviour of these flows.
o
documentation comments 🙂
t
Now, I know you want to ask: How would one indicate this behaviour? Again, I don’t have an answer, and I’m not suggesting the API needs to change. Just discussing.
o
I can't think of a good way to codify it, maybe someone more experienced with language development could propose something. For now, I'm satisfied with reading documentation
t
Yeah, or even just the flows variable/method name
g
such way to represent completing event source doesn’t exist in any reactive library which I know
actually even things like Single/Mono/Maybe do not exist in many libraries
t
Right. But I’m not quite sure what your point is. Why does it matter what other libraries do?
g
More people who think about such problems, nobody ended up with solution for now, so it probably doesn’t have simple/good enough solution
t
All the more reason to discuss it then
a
As soon as you start working with event based frameworks, there’s no such thing as “this 0..N will complete because of its type so I can safely put code *afterwards*” What you can do it keep going with the flow (no puns intended). Are you using any reactive library? Good, use it. Do you need something to be executed when the flow completes? Go for `onCompletion` method Are you in an Rx library? Then you have `doOnComplete` method As I read in a presentation, unless you can model everything synchronously, a single piece of asynchronous code can break your model. So model everything event based
t
Yeah, I think that’s good advice. But still, forget about putting code ‘afterwards’ because of its type. The same problem still exists with
onCompletion()
- without looking at the stream, you don’t know whether this will ever be called.
I’m not trying to argue that you should be able to infer that knowledge from the type. I am simply noting that you can’t.
a
But. as mentioned before, you can’t be sure that neither • the flow will emit a limited number of items so it will complete
• The flow will complete in a reasonable amount of time
t
That’s true. But we are left to look at the stream and draw these conclusions ourselves
a
Even if you had a type for that “CompletableFlow”, nothing prevents it from completing in several years
Then… I think that we’re only left with good ol’ “Use a proper name for your stuff” ¯\_(ツ)_/¯
t
Yes, I agree - in fact, I made all the same points. Again, I’m just lamenting that you can’t codify this somehow. The fact that a consumer can expect a flow to complete - if you can indicate that somehow in a variable name, then I’m just entertaining the notion that you could indicate that with a type.
a
If I see a
filesInFolder
method I know for sure that it will finish in a reasonable amount of time
databaseChanges
will never finish (unless the app gets closed and some cleanup needs to be done)
time
(not sure why, but just as an example) will be there forever
Totally agree, but that happens in a lot of contexts In the end, even codifyng it into a class ends up being just a name for it
t
Yeah, that’s exactly right
¯\_(ツ)_/¯
It’s a frustrating discussion because I’m not even advocating for this type
Just an interesting thought exercise
But yeah - I think the answer is that, if you can’t enforce this behaviour somehow, then there would be no point creating a type for it. If
CompletableFlow
was just a
Flow
with a different name, then you may as well just rename the variable/function.
a
In the end… I think that it’s not a matter of having an specific type or name for it. It’s a matter of the mental model that async libraries “impose” (I can’t think of a better word 😅) The same way any developer will not put anything in a finally after a
while(true)
in imperative programming, any developer with the asynchronous model in the head should not rely on putting anything after a
collect
You could go for a typealias to make it a bit for explicit
Copy code
typealias CompletableFlow<T> = Flow<T>
fun listFiles(): CompletableFlow<T>
But, otherwise, everything is the same
t
…should not rely on anything they put after collect being executed *
It’s OK to put code after collect - just as its OK to use
onComplete()
. But yeah, you have to have the expectation that it may never be called
Actually, a typealias is a really good suggestion for this
a
If you wanted to limit the “scope” of your
CompletableFlow
you could go for an inline class
Copy code
inline class CompletableFlow<T>(private val flow: Flow<T>)
So you can have functions that only accept a
CompletableFlow
Copy code
val flow: Flow<Int> = ...
val completable: CompletableFlow<Int> = ...
fun demo(c: CompletableFlow<Int>) = ...
demo(completable) // works
demo(flow) // doesn't work
I’m not sure about inline classes and generics, though
j
Timeout! Guaranteed to complete.
😂 1
z
Now, I know you want to ask: How would one indicate this behaviour?
I think this would be a great use case for dependent types, which kotlin doesn't have and probably never will. https://en.m.wikipedia.org/wiki/Dependent_type
👍 1
g
I think it’s overkill to solve this with dependent types
z
Is it? This thread is a discussion of how there isn't really a good way to express the "completability" of a flow in the type system. And expressing stuff like list size in the type of the list is a basic use case for dependent types, isn't it?
g
Even if something is completable in theory, it doesn’t make it completable on practice
also such things as error in potentially infinite stream also “complete” it if we talk about use case when you do something after “collect” method call
I mean it’s interesting theoretical talk for types, but I really don’t think it solvable on practice But I may be wrong
z
I don't know what the solution would look like, my (very limited) exposure to DTs is with pure functional languages that don't have imperative calls at all, let alone suspending ones. But as a pattern match, the problem of "how to express some runtime property in the type system" sounds like a dependent types problem to me.
t
Thanks Zach. I won’t pretend to understand anything in this article, but it’s nice to know that a language feature exists out there that might solve at least similar problems to the one I’m describing