What are coroutines really in a more technical poi...
# coroutines
t
What are coroutines really in a more technical point of view?
j
switch statements?
⏸️ 1
☝️ 3
👌🏼 1
❤️ 1
1
😅 1
t
@ephemient Coroutines are a bit confusing to be honest 🥴
e
"switch statements" is fair. compiler rewriting your linear code into switch statements jumping to program states broken up at every suspend point
j

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

👆 1
1
e
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
@ephemient thanks for this, I will watch it later 😄
u
syntactic sugar over nested callbacks
n
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)