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
kevin.cianfarini
05/11/2023, 10:52 PM
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
Interesting read, thanks. I suppose there aren’t any KEEPs or youtrack issues where people are discussing a solution specifically for kotlin?
e
ephemient
05/12/2023, 12:31 PM
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)
ephemient
05/12/2023, 12:44 PM
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)