https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hiring-french
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
l

LoganDark

06/23/2020, 10:40 AM
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
😒uspend: 1
r

Robert Jaros

06/23/2020, 10:47 AM
I would love to read an article with a comparison :-)
l

LoganDark

06/23/2020, 10:48 AM
@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!
v

vaskir

06/23/2020, 10:49 AM
"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?
l

LoganDark

06/23/2020, 10:50 AM
@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...
t

Tijl

06/23/2020, 10:54 AM
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
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 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.
kotlin*x*.coroutines
is just one possible high level implementation on top of that.
l

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!
I still don't know how to construct a proper Continuation to make
createCoroutine
behave though! Is there any other way to start a suspend function?
t

Tijl

06/23/2020, 11:03 AM
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.
l

LoganDark

06/23/2020, 11:04 AM
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...
m

marstran

06/23/2020, 11:05 AM
I think you should rethink your approach and try to use kotlinx.coroutines. Maybe you could use actors to achieve what you want?
z

Zach Klippenstein (he/him) [MOD]

06/23/2020, 11:05 AM
There are a few extension methods on
CoroutineScope
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.html
👍 1
m

marstran

06/23/2020, 11:05 AM
Using Continuations directly could get pretty complicated.
t

Tijl

06/23/2020, 11:05 AM
nope, it’ll be a journey alright. remember the slightly less radical alternatives that were suggested.
r

Robert Jaros

06/23/2020, 11:05 AM
@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
m

marstran

06/23/2020, 11:05 AM
I agree with @Zach Klippenstein (he/him) [MOD]
d

diesieben07

06/23/2020, 11:07 AM
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).
l

LoganDark

06/23/2020, 11:09 AM
So a Continuation is for what happens at the very end of the function?
d

diesieben07

06/23/2020, 11:09 AM
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
Yes
l

LoganDark

06/23/2020, 11:09 AM
So if my function is only a callback and the result doesn't matter, I can just have a Continuation that does nothing?
d

diesieben07

06/23/2020, 11:09 AM
I don't think I follow that question
l

LoganDark

06/23/2020, 11:10 AM
I don't care about the result so can I just do nothing with it?
d

diesieben07

06/23/2020, 11:10 AM
Uh, sure, you could. But that will mean any exceptions your suspending functino throws vanish into thin air
l

LoganDark

06/23/2020, 11:11 AM
Oh, that makes sense! Sorry, never dealt with continuations before!
m

marstran

06/23/2020, 11:12 AM
Also, remember that Kotlin continuations are one-shot. Be careful not to call them twice. Weird things will happen 😛
l

LoganDark

06/23/2020, 11:12 AM
Wasn't gonna! Hehe!
d

diesieben07

06/23/2020, 11:13 AM
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
suspendCoroutineUninterceptedOrReturn
, which gives you a
Continuation
. you can then resume that continuation later in your scheduler to resume the function
m

marstran

06/23/2020, 11:14 AM
l

LoganDark

06/23/2020, 11:15 AM
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!
a

araqnid

06/23/2020, 11:16 AM
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?
d

diesieben07

06/23/2020, 11:16 AM
Well, in Lua you have the advantage that the actual runtime supports continuations
In Kotlin coroutines are implemented entirely in the compiler
l

LoganDark

06/23/2020, 11:17 AM
Well, for the record my issue is solved for now, I will let you know if I need any more help! Thank you!
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
t

Tijl

06/23/2020, 12:21 PM
glad it worked out, nice to see more users of the low level API
l

LoganDark

06/23/2020, 12:22 PM
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!
g

gildor

06/23/2020, 5:05 PM
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
r

raulraja

06/26/2020, 4:54 PM
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
Continuation
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.