What is "CoroutineScope.()" in the documentation?


How do I have to read and interpret this function-signature?

What I don't get is " ... CoroutineScope.() ... ". I know that "() -> Unit" is a type-definition for a lambda. But what's "CoroutineScope.()" ? How is that meant?

public fun LaunchedEffect(
    key1: Any?,
    block: suspend CoroutineScope.() -> Unit
): Unit

See also: Jetpack Compose API Reference

2
Sep 6 at 6:29 AM
User Avatarcluster1
#android#kotlin#android-jetpack-compose

Accepted Answer

It means that the parameter must be a member function of CoroutineScope. This ensures it has access to other CoroutineScope functions like launch() inside of it. So how this works with a standard lambda is that the compile will see the lambda, see that the function expects a CoroutineScope member function, and create an extention function that has the body of the lambda in it, and pass that to the function.

You can see this in your code. Put a breakpoint in the lambda function and look at the value of 'this'. You'll see that this is a CoroutineScope.

User AvatarGabe Sechan
Sep 6 at 6:50 AM
2