Theoretical problem: is there any mechanism to clo...
# coroutines
y
Theoretical problem: is there any mechanism to close a resource upon suspension and reopen it open resumption? E.g if I have a file and I want to open it and line-by-line do some processing. That processing includes suspend functions, and so I'd like to close the file when they suspend as to not leave the file open for too long.
s
I guess you could make a custom continuation interceptor
Wrap the continuation before passing it to the dispatcher, and you can add advice around it
y
That would only work if the call is ever `intercept`ed though, no? I don't think every suspension is guaranteed to intercept the continuation, especially with the presence of
suspendCoroutineUninterceptedOrReturn
. Or is there a contract I'm unaware of here that every suspension should be resumed with
intercepted
?
s
If you're using the coroutines library, everything is intercepted. The only exception is
suspend fun main()
since it has no dispatcher.
The interceptor is how dispatchers and thread context elements work, so things would break without it
thank you color 1
h
BTW do you plan to open source the file implementation?
y
I don't actually have code for any of this. This is a theoretical problem that other languages have to contend with and I was wondering how Kotlin coroutines handles it