Is it theoretically possible that an interface wit...
# coroutines
m
Is it theoretically possible that an interface with a
suspend fun
could have a class that implements it and converts it to just a regular
fun
? The implementing class would simply ignore the
suspend
keyword.
k
Since a continuation is added at compile time to function signatures I doubt it. However I think you've be interested in the research going into Rust to make keywords (like
async
) generic. https://blog.rust-lang.org/inside-rust/2022/07/27/keyword-generics.html
m
Interesting read, thanks. I suppose there aren’t any KEEPs or youtrack issues where people are discussing a solution specifically for kotlin?
e
you can somewhat do this already with
Copy code
interface Suspending {
    suspend fun get()
}
abstract class Immediate : Suspending {
    abstract fun immediate()
    final override suspend fun get() = immediate()
}
but the two functions need different names (even though technically their platform signatures don't collide)
or if you don't mind being unsafe, a wrapper like
Copy code
fun <T> runUndispatched(block: suspend () -> T): T {
    val scope = CoroutineScope(Dispatchers.Unconfined)
    val deferred = scope.async { block() }
    scope.cancel()
    return deferred.getCompleted()
}
and as long as
block
returns a value without actually suspending, it'll work (otherwise it'll throw)
w
I like this thanks. So sick of the word safe!