Is there something like `SuspendCloseable`? :think...
# announcements
m
Is there something like
SuspendCloseable
? 🤔 I have to delete a temporary file in
close()
but don’t want it to be a blocking call.
z
There is not. Just launch a new coroutine to do the io work.
m
That's exactly what I want to avoid. Users of my library API aren't supposed to remember that in only two cases (
close()
and
use()
) they have to dispatch to another context while everything else are suspend functions already. Also it messes up the code because it requires adding a wrapping boilerplate at every single call site.
z
I don't understand how this would require your users to do anything - I thought you meant you are exposing a
close
method in which you want to do something with a temp file, but not block the caller. In that case, you could launch a coroutine internally to do that.
m
That would change the behavior. Exceptions would no longer be connected to the call site but happen somewhere in the background. Also the caller is supposed to wait for the cleanup to complete. Also, structured concurrency won't work properly if I use suspend fun -> non suspend fun (close) -> runBlocking -> suspend fun
z
Yep. So you don't want it to be synchronous/blocking, and you also don't want it to be asynchronous… not sure what other options there are 😅
m
I want it to be suspending, just like all other API 🤔 I can make it both, asynchronous and synchronous, but in no case it's suspending.
z
Ah I see what you mean. And providing your own closeable interface isn't an option?
m
It is, but I was wondering if there already is a standard solution for coroutines, or at least one planned :)