kotlinasynchronouskotlin-coroutines

Are Kotlin coroutines colored?


Somewhat famous article about state of asynchronous programming model of many languages, states that they have a "color" problem, which, in particular, divides the ecosystem into two seperate worlds: async and non-async. Here are properties of such a language:

Kotlin is pretty new language, so I thought I should look into its asynchronous model. However, Kotlin transpiles to JavaScript, so I believe it has color problem more than I believe it doesn't. But its coroutines were a bit hard for me to gross and got confused, so I'm here asking Which of those properties are true for Kotlin? (and how much of color problem it solved).


Solution

  • Yes.

    On the syntactic level, No. It's just a plain call, and it returns with the result as usual. However, on the bytecode level, Yes, calling a suspend fun is entirely different business.

    Yes.

    Yes, but they are there to help you with the red function business. You are never forced to use them to get some basic functionality.

    Also true, but there's a lot of support to make the pain quite low. You can just use runBlocking { } anywhere to "ascend into the red world", and you can color the entry point itself red by just writing suspend fun main(). Another good choice of Kotlin, not seen in many other languages, is that the await-like behavior is built into the function itself, you don't have to write myFunction().await().

    In practice, the most painful aspect of Kotlin Coroutines is that they can't remove blocking from the underlying APIs. For example, it's pretty easy to slip into using Java File IO, which is blocking, and freeze the progress of all the other coroutines using the same thread. It is also pretty hard for the compiler to determine when you're doing this, so you find it out the hard way.