https://kotlinlang.org logo
#coroutines
Title
# coroutines
h

halim

04/28/2019, 9:19 AM
hi
Copy code
i am confuse with this syntax 
fun task(bloc: suspend () -> Unit) { .... }
what does mean call suspend function inside regular function ?
o

octylFractal

04/28/2019, 9:21 AM
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

halim

04/28/2019, 9:22 AM
not clear, can you make an example to explain your idea
s

simon.vergauwen

04/28/2019, 9:22 AM
There is no way to know that. You can run suspend functions using
startCoroutine
with a callback.
h

halim

04/28/2019, 9:23 AM
yes i know about how to call this suspend func but why declare it suspend in regular fun ??
s

simon.vergauwen

04/28/2019, 9:23 AM
Well it could be a fire and forget scenario.
h

halim

04/28/2019, 9:24 AM
more details i possible
s

simon.vergauwen

04/28/2019, 9:25 AM
task runs the suspend bloc function with a callback using startCoroutine.
o

octylFractal

04/28/2019, 9:25 AM
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

simon.vergauwen

04/28/2019, 9:25 AM
That's one of the many possible scenarios
o

octylFractal

04/28/2019, 9:26 AM
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

halim

04/28/2019, 9:35 AM
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

octylFractal

04/28/2019, 9:38 AM
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

halim

04/28/2019, 10:00 AM
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

gildor

04/28/2019, 5:06 PM
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

halim

04/29/2019, 11:07 AM
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

gildor

04/29/2019, 11:08 AM
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

halim

04/29/2019, 11:10 AM
yes exaalty what i want to say
g

gildor

04/29/2019, 11:11 AM
Okay, but how this related to your original question than?
h

halim

04/29/2019, 11:12 AM
its not depend, i will read a topic about kotlin/keep
thanks for your time
2 Views