Why are Kotlin coroutines so complicated? I'm use...
# coroutines
Why are Kotlin coroutines so complicated? I'm used to Lua coroutines, which are simple: You can resume them at any time, from any thread, and they run (like a function call) until the next yield! Timed waiting (like delay/sleeps) is very simple, just tell a task scheduler to resume your code in the future, and then yield! But Kotlin coroutines are different... They're weird! I've been trying and failing for hours to imitate Lua coroutines with Kotlin but everything is so different that it just makes no sense! So how am I supposed to do my thing? My application has an event loop which calls a function every frame, and I want to manually resume coroutines that meet certain criteria every frame, so that they run on my thread, inside my function, but it's so complicated! Dispatchers give me no control over when the code is executed, or where, and I need it inside that specific function or internal state will be messed up! I've spent so long even trying to get a proper instance of CoroutineScope, and now that I have one (which probably won't even work because I haven't specified a dispatcher, but I haven't been able to test my code for hours because I can't even code up a basic test), I don't even know how to invoke createCoroutine, or if that's the right function to invoke, because I can't come up with a magic instance of Continuation and everything is so complicated and I'm confused and please help me!!
⏸️ 1
👀 1
I would love to read an article with a comparison :-)
@Robert Jaros Here's a Lua task scheduler! https://pastebin.com/x24ABbSi
That kinda stuff is what I'm used to! Not this structured/etc stuff!
"I've been trying and failing for hours to imitate Lua coroutines with Kotlin" - why did you think it's 1. make sense 2. possible?
@vaskir Well, it's because it's what I know and I'm used to it! But clearly that won't work so that's why I came here! To learn how to do it properly I guess!
I don't understand this structured stuff, the tutorials are pretty basic and don't cover my use case! So I don't know what to do other than ask for help...
Timed 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
? 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 executed
but 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.
is just one possible high level implementation on top of that.
“simple” as opposed to delay(x)?
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!
I still don't know how to construct a proper Continuation to make
behave though! Is there any other way to start a suspend function?
ok, that’s a deep dive! good luck and yeah since 99,9x% of users are probably not using this directly, you’ll probably have to find out a lot on your own. the source of coroutinex might be handy, but it’s kind of weighty. I don’t even know of any other project that uses the low level API directly.
Well, I don't even know where to start! I 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 think you should rethink your approach and try to use kotlinx.coroutines. Maybe you could use actors to achieve what you want?
There are a few extension methods on
that are the standard ways of launching a coroutine.
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.html
👍 1
Using Continuations directly could get pretty complicated.
nope, it’ll be a journey alright. remember the slightly less radical alternatives that were suggested.
@LoganDark perhaps it would be easier for your use case to create a single thread dispatcher with your thread and just use it with your suspending functions
I agree with @Zach Klippenstein (he/him) [MOD]
Continuation is basically a callback. When you invoke a suspend function, it will invoke the continuation with it's result when its done instead of returning it (because that would be blocking).
So a Continuation is for what happens at the very end of the function?
A good starting point to understanding the underlying machinery imho is the
iterator { yield() }
builder. It uses kotlin.coroutines and shows how to manually start and continue from a coroutine
So if my function is only a callback and the result doesn't matter, I can just have a Continuation that does nothing?
I don't think I follow that question
I don't care about the result so can I just do nothing with it?
message has been deleted
Uh, sure, you could. But that will mean any exceptions your suspending functino throws vanish into thin air
Oh, that makes sense! Sorry, never dealt with continuations before!
Also, remember that Kotlin continuations are one-shot. Be careful not to call them twice. Weird things will happen 😛
Wasn't gonna! Hehe!
Also note that by default there is no yielding. you have to write that yourself (yield is just a suspending function that the coroutine-function can call)
And that yield would basically just call
, which gives you a
. you can then resume that continuation later in your scheduler to resume the function
Yes, I get it thanks! Like 3 messages ago! I'll ask again if I need more help! I already told you I know how task scheduling works, silly!
🤪 1
I just needed help starting the coroutine and understanding what a continuation is! And now I know! Including all of the side effects, don't worry about it! Hehe!
After all I wrote that little task scheduler I linked!
https://github.com/araqnid/kotlin-hoist-regex/blob/master/compiler-plugin/src/main/kotlin/org/araqnid/kotlin/hoistregex/kotlin/MatchingCoroutine.kt — here’s an example of where I wrote a sequence matcher using kotlin.coroutines that sounds like it may be more the sort of example you’re looking for?
Well, in Lua you have the advantage that the actual runtime supports continuations
In Kotlin coroutines are implemented entirely in the compiler
So it turns out everything worked pretty much exactly as expected as soon as I figured out how to do things correctly! Here's the code now: https://pastebin.com/nUaRAa0T
So Kotlin coroutines can be used a lot like Lua coroutines! I wasn't completely wrong! Hehe!
I experienced about 0 trouble with yielding by the way! @diesieben07
glad it worked out, nice to see more users of the low level API
Still had to include kotlinx for the SupervisorJob because that's actually a big convenience, but I think that's the only thing I used!
I 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
Part of the confusion is that the library KotlinX Coroutines models a framework for async/concurrency but the standard lib models delimited continuations. Yet the standard lib uses both words
for the interface and
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
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.