Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are continuations in Kotlin usable yet? Any examples available?

There is an package in Kotlin for continuations, but it's marked as experimental. There is no documentation other than the API, and no tutorial or examples anywhere that I could find. Does anyone know if it's already usable? What would be an example of its usage?

like image 435
user118967 Avatar asked Dec 17 '25 15:12

user118967


2 Answers

The Continuation interface is a part of coroutines support API in the standard library. You can start exploring the coroutines from the documentation page, or from the kotlinx.coroutines library repository, which contains an extensive coroutine programming guide.

Coroutines are experimental in Kotlin 1.1 and 1.2, but there was an announcement that they are going to stabilize in 1.3.

like image 66
Ilya Avatar answered Dec 20 '25 04:12

Ilya


When you say "continuations", you actually mean "coroutines". Continuation is a part of that story.

kotlin-coroutines-informal by the JetBrains team is a great resource to start you off with coroutines. If you're looking to use them for async programming on Android, especially take note of the section on wrapping the callbacks which your existing async API provides, turning the existing Java function calls into Kotlin suspend funs.

About the status of experimental, check out Roman Elizarov's answer to that question. Here's a highlight:

Kotlin coroutines can and should be used in production. That was the chief reason to officially release them in Kotlin 1.1. Having released them, the JetBrains team had committed to maintain backwards compatibility with respect to any changes that are introduced to them in the minor releases as they evolve, while allowing people to safely try them in complex production applications.

There is absolutely no reason to wait for 1.3 to start using coroutines. Whatever you write today will work into the foreseeable future with no changes and, on top of that, it will be very easy to switch from kotlinx.coroutines.experimental to kotlinx.coroutines after the release. The APIs are already very stable and most of the changes are now in the area of channels and actors.

like image 30
Marko Topolnik Avatar answered Dec 20 '25 05:12

Marko Topolnik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!