From my understanding, @RestrictsSuspension defines what suspending functions can be called in a function block. And I'm looking at this code and trying to understand how a @RestrictsSuspension receiver works with a suspending lambda.
A suspending lambda is passed to a function that has @RestrictsSuspension receiver. This annotated receiver says that only its own suspending methods may be called. Yet the passed lambda is suspending, meaning it cannot be called. And this, to me, makes no sense: we're passing a suspending lambda we cannot call.
@RestrictsSuspension
class SomeReceiver {
fun doSomething() {
println("fun doSomething()")
}
}
suspend fun f1(suspendingLambda: suspend SomeReceiver.() -> Unit) {
val receive = SomeReceiver()
receive.fn() // WILL CAUSE AN ERROR
}
fun main() = runBlocking<Unit> {
launch {
fn1 {
//
}
}
}
This gives the error Restricted suspending functions can only invoke member or extension suspending functions on their restricted coroutine scope. And this makes sense since I annotated SomeReceiver with @RestrictsSuspension.
This, obviously, is a contrived example that I used to try to understand the concepts. The actual code never directly calls the passed suspending lambda. Within android's source code it does this:
suspendingLambda.createCoroutine(handlerCoroutine, handlerCoroutine).resume(Unit)
So I'm obviously missing something knowledge about coroutines to understand how this can work.
My question is: how I can use @RestrictsSuspension receiver to restrict the suspending functions in a block, while also passing in a suspending lambda?