LoganDark
06/23/2020, 10:40 AMRobert Jaros
06/23/2020, 10:47 AMLoganDark
06/23/2020, 10:48 AMvaskir
06/23/2020, 10:49 AMLoganDark
06/23/2020, 10:50 AMTijl
06/23/2020, 10:54 AMTimed waiting (like delay/sleeps) is very simple, just tell a task scheduler to resume your code in the future, and then yield!“simple” as opposed to
delay(x)
?
the coroutine API for Kotlin is much more high level as this example illustrated. Of course trying to simulate a lower level API with a higher level API is typically troublesome.
If you want to do this as you say:
Dispatchers give me no control over when the code is executedbut by that you mean the included Dispatchers, which again have a much higher level of abstraction, usually not even concerning themselves with the concept of threads. If you write your own Lua like dispatcher you’ll get a lot closer to what you want. But there will always be differences (e.g. exact control over when you yield will remain frustrating) There is actually an even lower level abstraction, `kotlin.coroutines` (note, no *x*) which governs how suspend functions work.
kotlin*x*.coroutines
is just one possible high level implementation on top of that.LoganDark
06/23/2020, 10:56 AM“simple” as opposed to delay(x)?All
delay
would be, in that case, is a function which does the same thing! Usually you don't do it manually, nope!
There is actually an even lower level abstraction, kotlin.coroutines (note, no x) which governs how suspend functions work.Oh, I saw that before! I was surprised at the number of bloat the kotlinx one has! I will drop the kotlinx and check out the stdlib coroutines then!
createCoroutine
behave though! Is there any other way to start a suspend function?Tijl
06/23/2020, 11:03 AMLoganDark
06/23/2020, 11:04 AMmarstran
06/23/2020, 11:05 AMZach Klippenstein (he/him) [MOD]
06/23/2020, 11:05 AMCoroutineScope
that are the standard ways of launching a coroutine. launch
is the most basic one. Have you seen the official coroutine guide? I would recommend learning kotlin coroutines with the assumption that they're entirely different from what you're used to in Lua, and only then thinking about how to compare them to what you're used to and if you need to emulate that, let alone how to do so.
Checkout the guide first though:
https://kotlinlang.org/docs/reference/coroutines/coroutines-guide.htmlmarstran
06/23/2020, 11:05 AMTijl
06/23/2020, 11:05 AMRobert Jaros
06/23/2020, 11:05 AMmarstran
06/23/2020, 11:05 AMdiesieben07
06/23/2020, 11:07 AMLoganDark
06/23/2020, 11:09 AMdiesieben07
06/23/2020, 11:09 AMiterator { yield() }
builder. It uses kotlin.coroutines and shows how to manually start and continue from a coroutineLoganDark
06/23/2020, 11:09 AMdiesieben07
06/23/2020, 11:09 AMLoganDark
06/23/2020, 11:10 AMdiesieben07
06/23/2020, 11:10 AMLoganDark
06/23/2020, 11:11 AMmarstran
06/23/2020, 11:12 AMLoganDark
06/23/2020, 11:12 AMdiesieben07
06/23/2020, 11:13 AMsuspendCoroutineUninterceptedOrReturn
, which gives you a Continuation
. you can then resume that continuation later in your scheduler to resume the functionmarstran
06/23/2020, 11:14 AMLoganDark
06/23/2020, 11:15 AMaraqnid
06/23/2020, 11:16 AMdiesieben07
06/23/2020, 11:16 AMLoganDark
06/23/2020, 11:17 AMTijl
06/23/2020, 12:21 PMLoganDark
06/23/2020, 12:22 PMgildor
06/23/2020, 5:05 PMI don’t even know what a continuation is let alone how to make one! There’s no example code, no documentation longer than a couple terse sentences...I recommend to check official design document if you need implementation details https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md
raulraja
06/26/2020, 4:54 PMContinuation
for the interface and CoroutineContext
for its context. Delimited continuations as pointed out by @gildor in that doc can do other things beside async and concurrency. For example the sequence builder in the std lib, a datatype that does not use async or concurrency. There is a problem though. The ContinuationImpl
in the JVM uses an internal stack that does not support multi-shot continuations. You see some data types like Flow or the ones in Arrow work around that either hacking it or extending the actual classes and creating custom continuations.
In an ideal world the standard lib would support multishot continuations and cancellation out of the box so that other libraries like Arrow Fx Coroutines and KotlinX Coroutines or any user that uses suspension/continuations for other purposes would have transparent interop.