https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

therealbluepandabear

03/23/2021, 7:41 PM
What are coroutines really in a more technical point of view?
j

jw

03/23/2021, 7:52 PM
switch statements?
⏸️ 1
☝️ 3
👌🏼 1
❤️ 1
1
😅 1
t

therealbluepandabear

03/23/2021, 7:54 PM
@ephemient Coroutines are a bit confusing to be honest 🥴
e

ephemient

03/23/2021, 7:54 PM
"switch statements" is fair. compiler rewriting your linear code into switch statements jumping to program states broken up at every suspend point
j

jw

03/23/2021, 7:54 PM

https://www.youtube.com/watch?v=YrrUCSi72E8

👆 1
1
e

ephemient

03/23/2021, 7:56 PM
I was going to say, try writing your own coroutines by hand first and then you'll understand why kotlin compiler does what it does. but I guess that talk summarizes that part too
😀 1
t

therealbluepandabear

03/23/2021, 8:19 PM
@ephemient thanks for this, I will watch it later 😄
u

ursus

03/23/2021, 8:55 PM
syntactic sugar over nested callbacks
n

Natsuki(开元米粉实力代购)

03/24/2021, 6:17 AM
IMHO 1. every suspend point split the original code into two parts of function (the previous part and the rest part), and compiler may optimize and implement this in a state-machine 2. since a suspend function is composed of two or more split function, you can decide whether to call the rest function or just return on the suspend point , 3. you can call the rest function immediately there's no significant difference from the not-split one 4. you can also just return and only call the rest function when some condition meets (i.e network request done) 5. compared to the original one, now you have a chance to decide to call the rest function, that's the point coroutine is, but it still looks like a aotimic function in literal code (at lest in source code level)
10 Views