3

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?

user118967
  • 4,895
  • 5
  • 33
  • 54

2 Answers2

2

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.

Ilya
  • 21,871
  • 8
  • 73
  • 92
1

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.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Thanks. Reading the material, it seems to me coroutines use continuations but continuations are not exposed to applications in the same way they are exposed in Scheme, Scala (reset and shift), Haskell, etc, correct? I was particularly interested in replicating the non-deterministic example shown here: https://gist.github.com/sebfisch/2235780 that is, I wanted to find out if there was anything in Kotlin equivalent to capture and escape as used in the article. This seems to be corroborated by the comment in the 'informal' linked document that continuations cannot be resumed more than once. – user118967 Sep 11 '18 at 01:14
  • They are definitely exposed to the application, [see this answer](https://stackoverflow.com/questions/51566006/how-to-suspend-a-coroutine-at-a-specific-point/51568707#51568707) for a basic example. It's semantical nonsense to resume a continuation twice because it corresponds to the computation going on from the suspension point. You can't roll back to the past. When the coroutine suspends again, you get another continuation object. Other languages do the same, just have different definitions of concepts. – Marko Topolnik Sep 11 '18 at 07:48
  • As far as your gist link, I find that explanation terribly convoluted, there are far better ways to explain those concepts. It's material only for a highly motivated enthusiast. I haven't spent much time on the nondeterministic part, but I've seen something similar implemented with Ruby. Basically it's some sort of backpropagation constraint solving. I haven't seen an equivalent in Kotlin, but it would be an interesting exercise to see if it would work with coroutines. – Marko Topolnik Sep 11 '18 at 07:57
  • Thanks, Marko. Interesting, I found the gist explanation one of the best on continuations I have found so far. :-) In any case, I am not sure about your statement that "it's semantical nonsense" to run a continuation twice. In fact, my understanding is that that is essential for nondeterminism because you are running the same continuation for two different alternative values. People sometimes refer to continuations as a way to "travel to the past" and "time travel": https://blog.knoldus.com/time-travel-in-scala-cps-in-scala-scalas-continuation/ – user118967 Sep 11 '18 at 17:43
  • I should probably soften my statement that running continuations twice are *essential* for non-determinism. Indeed, that seems to be the most common use case for them. However, I see references to getting non-determinism using generators. Searching for "non-determinism with generators" returns some links, but I still need to digest them. – user118967 Sep 11 '18 at 17:48
  • The "semantical nonsense" pertains to Kotlin's definition of a continuation, which is in my opinion a cleaner one. You can't travel to the past, it's just an alegory. Between your continuation calls the world has changed, the continuation itself may have changed it (side effects), so you're just re-entering the same code in a new world. The state it has captured in its call stack may be outdated. But in Kotlin you can arrange a loop in the coroutine so that you get a very similar effect, but more explicit. You suspend the coroutine in each iteration. – Marko Topolnik Sep 11 '18 at 18:02