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
Sam
08/08/2024, 5:50 PM
I guess you could make a custom continuation interceptor
Sam
08/08/2024, 5:50 PM
Wrap the continuation before passing it to the dispatcher, and you can add advice around it
y
Youssef Shoaib [MOD]
08/08/2024, 5:53 PM
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
Sam
08/08/2024, 5:55 PM
If you're using the coroutines library, everything is intercepted. The only exception is
suspend fun main()
since it has no dispatcher.
Sam
08/08/2024, 5:57 PM
The interceptor is how dispatchers and thread context elements work, so things would break without it
thank you color 1
h
hfhbd
08/09/2024, 1:53 PM
BTW do you plan to open source the file implementation?
y
Youssef Shoaib [MOD]
08/09/2024, 3:31 PM
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