Hello! We have just upgraded kotlin and coroutines...
# coroutines
e
Hello! We have just upgraded kotlin and coroutines for our service. One thing noted was that main can now be a suspend method. What happens if main gets suspended? Is there any considerations when having long-lived applications? (previously we did a runBlocking { application.start() }, now we just do application.start())
m
I'm pretty sure
suspend fun main() { ... }
is equivalent to
fun main() = runBlocking { ... }
.
e
thanks! how sure is pretty sure?
m
I think I've read it somewhere. Trying to find it in the docs now 🙂
e
awesome! thank you!
Do I need to explicitly add the coroutineScope? In the example we have "suspend fun main() = coroutineScope { ... }" and I would have expected just "suspend fun main() { ... }"
m
You need that if you want to start other coroutines within
main
. Like the `async`s in that example.
So yeah, that is a difference.
runBlocking
would create that scope for you.
e
I'm not sure I follow tbh. When would I/wouldn't I want that? What is the effect of either?
m
You need a
coroutineScope
to start coroutines like
async
,
launch
,
produce
and so on. It's part of something called "structured concurrency". You can read more about it here: https://medium.com/@elizarov/structured-concurrency-722d765aa952
e
ahh - of course. async and the others start new coroutines - I was confused with just calling other suspend functions. Thanks for the link, will give it a read!
m
Right. You don't need a scope if you just want to call other suspend functions.