K is for Kotlin

Coroutine is a new experimental feature coming in Kotlin 1.1. This is a framework for what they call “suspendable computation”.

Take for example asynchronous requests: we start something, then we wait for the result, and when the result is available we do some other stuff with it.

Let’s see the following example:

tweetService.getJobSuggestions { tweets ->

displayTweets(tweets)

}

Here we ask for tweets from a web service, and when they’re returned we display them in the UI. The UI thread is not blocked while we’re waiting, because network operations occurs in a different thread. However, if we’d use a synchronous API, the code would be clearer:

val tweets = tweetService.getJobSuggestions()

displayTweets(tweets)

…but we can’t do that because we would freeze the UI, whatever framework you’re using (Android, Swing, etc.).

With coroutines we will be able to have both advantages of these two snippets: non-blocking asynchronous calls, with clear and linear code.

This allows us to write the following code:

val tweets = tweetService.getJobSuggestions().await()

displayTweets(tweets)

I won’t explain how this works internally, but this code is strictly equivalent to the first snippet that was using a callback. We start a request, we wait for the result without blocking the UI thread, and then we update the UI. Also, we could add error management easily just by wrapping the service call into a good old try/catch.

Imagine now some real world use cases, when you need to call multiple web services, then write data to a database, and so on. All this stuff can now be written in a completely different way that looks more natural.

In fact this pattern is not new, it already exists in other languages (C#, Scala), and it’s often referred as async/await.

But the thing that’s interesting with Kotlin, it’s the fact that async/await is not part of the language itself. Only coroutines are, and they can be used for multiple purposes. Also, anyone can write his own async/await wrapper around any asynchronous library with a few lines of code. So, all your code that uses callbacks/futures/observables/whatever can now be transformed easily, whatever library you’re using. Spoiler: some work is already available for Rx and Java Futures.

With coroutines we can also implement generators. Maybe you’ve already heard of generators as they already exists in javascript with the yield keyword. It’s a special kind of functions that can return values and then can be resumed when they’re called again. Think about lazy, infinite streams of values, like the Fibonacci sequence:

// inferred type is Sequence<Int>

val fibonacci = buildSequence {

yield(1)

var cur = 1

var next = 1

while (true) {

yield(next)

val tmp = cur + next

cur = next

next = tmp

}

}

But the point with coroutines is that they allow writing cleaner code, mainly because there is no more callback hell preventing us for having a clear vision of what a function does.

Check the documentation if you want to know more about coroutines:

And the first coroutines libraries written by the Kotlin devs (wrappers for rx, futures, nio, swing, javafx…):