hi ``` i am confuse with this syntax fun task(blo...
# coroutines
h
hi
Copy code
i am confuse with this syntax 
fun task(bloc: suspend () -> Unit) { .... }
what does mean call suspend function inside regular function ?
o
That tells the compiler to make
bloc
a
suspend
lambda, i.e. you need to call
bloc
from a suspend function it doesn't matter if the parameter is on a regular function, because the function isn't calling
bloc
h
not clear, can you make an example to explain your idea
s
There is no way to know that. You can run suspend functions using
startCoroutine
with a callback.
h
yes i know about how to call this suspend func but why declare it suspend in regular fun ??
s
Well it could be a fire and forget scenario.
h
more details i possible
s
task runs the suspend bloc function with a callback using startCoroutine.
o
Copy code
fun <T> runSomethingTwice(block: suspend () -> T ): T = runBlocking {
  block()
  block()
}
this function calls
block()
, which may need to suspend (it uses some suspending resource) twice, using
runBlocking
to provide a CoroutineScope / suspend context
s
That's one of the many possible scenarios
o
it just means the lambda can't be used in a normal context, you need some suspending context to call it
in this case, I've used
runBlocking
to get such a context, but there are other options like
async
and
launch
h
so if i understand the purpose of use suspend inside regular is to avoid to blocking the caller thread of regular fun and wrap this suspend inside scope and dispatch in other thread
o
no, the regular function may still block (as in my case) but it could also hand it off to
launch
or
async
, in which case it wont
h
so if wrap regular fun in scope with (async, launch) it becomes async-fun, so how about implemenation of coroutine builder (launch, async, runbloking, flow) its not wrap in any scope just run suspend inside so how explain suspend usage in cooutine builder : launch, async, runblocking, flow ??
g
if wrap regular fun in scope with (async, launch) it becomes async-fun
It's incorrect, function is still syncronous and cannot suspend
so how about implemenation of coroutine builder (launch, async, runbloking, flow)
If you interested how it implemented check sources (or even better coroutines design reference which has simple veraions of those builders), under the hood they use low level coroutine builders provided by stdlib, which used to create coroutine from common callback based asyncronous code
This is more specific questions, how existing Coroutine builders are work, really recommend check this this document you want to know it in details https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md But your original question was about suspend lambda and as mentioned in this thread, there are many usecases and it not limited by coroutine builder. In general suspend lamdba is just a lamda that can be invoked only from suspend function, same way as suspend function
h
thanks for response, so how about this snapshot
Copy code
fun CoroutineScope.someCall() = launch {
   println("inside some call")
}

main() = runblocking {
   println("before")
   someCall()
   println("done")
}
when execute: before -> done -> inside some call so when wrap regular fun (someCall) in coroutine scope it become asynchrone
g
How this related to your original question?
Also of course it's asyncronous, because function someCall creates new coroutine that just started, this is completely different from your original snippet
h
yes exaalty what i want to say
g
Okay, but how this related to your original question than?
h
its not depend, i will read a topic about kotlin/keep
thanks for your time