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

Antanas A.

01/18/2020, 11:02 AM
Hi, is it technically possible to resume continuation twice when calling suspendCoroutine { cont -> cont.resumeWith(...) }?
d

Dominaezzz

01/18/2020, 11:06 AM
No! It must only be done once. The second time would throw an exception I think. Do you need this?
a

Antanas A.

01/18/2020, 11:18 AM
I am just thinking what capabilities would emerge if multiple returns would be possible. It will work as some kind of re-entrant call of function (which is parent program), it's hard to imagine mentally, but if it's possible maybe we could create something like Jetpack Compose without compiler addons.
d

Dico

01/18/2020, 11:20 AM
Definitely not supported and dangerous
a

Antanas A.

01/18/2020, 11:34 AM
why it would be dangerous?
By reading some examples where CPS could be applied there are interesting cases: as backtracking or others:
s

streetsofboston

01/18/2020, 5:01 PM
Use Flow for multiple value emissions. If you're thinking about for-comprehension like or do-notation like functionality, the folks from Arrow actually did make a form of multi-shot continuation possible. Not sure if their current Fx library still does that or now uses a different approach.
d

Dico

01/18/2020, 5:49 PM
Not considering Arrow, coroutine code abstracted by continuation contains a state machine based on your code, which is predicated on running exactly once. Therefore, if it runs more than once on the same state machine you get undefined behaviour.
☝️ 2
21 Views