Dmitry Khalanskiy [JB]
03/24/2025, 11:05 AMJoffrey
03/24/2025, 11:06 AMrunBlocking. This should be considered an advanced / delicate API, and shouldn't appear in a beginner's guide IMO.Dmitry Khalanskiy [JB]
03/24/2025, 11:09 AMrunBlocking. The only alternatives I can think of are suspend fun main() { coroutineScope { ... } } and GlobalScope.launch { }; Thread.sleep(1000). Both of them have their own weaknesses. Do you maybe have something specific in mind?Joffrey
03/24/2025, 11:14 AMGlobalScope either. suspend fun main (+ optionally coroutineScope { ... } when necessary) was what I had in mind. What weaknesses do you see for this approach? If we expect users to just accept runBlocking as an entrypoint, we may as well make them accept that version instead.
The problem with runBlocking is that it is transposable to many other places than main, and we clearly don't want users to do this unless they think really hard about what that means. With suspend fun main, they can't transpose it, which is good.phldavies
03/24/2025, 11:17 AMrunBlocking whenever they need to call something suspend from a non-suspend context (i.e. in a close for an AutoCloseable)Joffrey
03/24/2025, 11:20 AMrunBlocking, so we may as well avoid it in the guide.
I opened this issue a while back, and one of the pushbacks was that a lot of materials use runBlocking. Let's start by fixing the official learning materials.uli
03/24/2025, 11:26 AMval socket = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
Socket("localhost", 8080)
}
socket.use {
// ...
}
Would be great if this issue was more widely known. And even more great, if it was clear how to do such allocations right.CLOVIS
03/24/2025, 6:00 PMuli
03/24/2025, 6:05 PMlouiscad
04/11/2025, 6:37 AMDmitry Khalanskiy [JB]
04/11/2025, 6:49 AMlouiscad
04/13/2025, 2:40 PMlouiscad
04/13/2025, 2:45 PMDmitry Khalanskiy [JB]
04/16/2025, 2:13 PMlouiscad
04/16/2025, 2:15 PM