hi guys, what type of programming feature that ena...
# coroutines
a
hi guys, what type of programming feature that enables us to use
launch
and
delay
functions inside coroutine?
Copy code
fun main() = runBlocking { // this: CoroutineScope
    launch { // launch a new coroutine and continue
        delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
        println("World!") // print after delay
    }
    println("Hello") // main coroutine continues while a previous one is delayed
}
r
Which kinda name are you looking for? red/blue functions?
suspend
? Coroutines?
a
no, the
launch
and
delay
methods, why we can access them?
r
Ah, Kotlin allows you to set a scope for blocks, so
lauch
is basically
Copy code
kotlin
runBlocking { scope ->
  scope.launch { ...
AFAIK
Ah right, markdown in Slack is still barebones 😞
a
isee, so it’s actually the property of the first parameter in lambda functions.. is it only for first parameter that we can refer its property or for any parameters? could u point me to some docs?
r
I think you gotta do some more than just first parameter, but don't have any docs at the ready.
a
it’s probably called extension function,, which extends
CoroutineScope
🤔🤷‍♂️
r
I think you might be looking for the term "function literal with receiver". The
runBlocking
function takes such an argument (the code block that you supply) in the form
block: suspend CoroutineScope.() -> T
. So inside the code block you provide to the
runBlocking
function,
CoroutineScope
becomes the implicit
this
, meaning that you can call functions from the
CoroutineScope
class, like
launch
and
delay
. The
runBlocking
function then runs your code block, in the context of an instance of
CoroutineScope
.
l
Note that
delay
is just a regular suspend function.
💡 1